Testing JTidy

JTidy provides some helper classes to test its functionalities. Having a wide test coverage will help JTidy developers to assure code quality along releases. If you want to write a test case for JTidy (please do that if you are a developer and you find a JTidy bug) you can follow few simple guidelines.

See the test xref report for test examples.

Writing test cases


To write a JTidy test case your test class must extends org.w3c.tidy.TidyTestCase (you can find in the src/test ) directory. This class offers different utility methods to load files and evaluate results.

Input file for testing should be placed in the src/test-resources directory. An optional configuration file can also be supplied: the file name must be the same name of the input file with the ".cfg" extension.

Test for crash or loops

For testing an input file which causes a NPE or an infinite loop, simply calls executeTidyTest(String fileName) where filename is the simple (without path) file name for your input file. This method will take care of loading the configuration (if a .cfg file with the same name of the input file exists) and call tidy.parse() .

Testing output

Place a file with the same name of your input file but with the ".out" extension containing the expected result. Call executeTidyTest(String fileName) and the result will be automatically compared with the supplied file. An AssertionException will be thrown if files are different (the exception will include the different lines from both files).

Testing parser

Call parseDomTest(String fileName) : this will return a Document object so you can make assertions on its content.

Testing errors/warning

Call executeTidyTest(String fileName) method and check the number of warning/errors on the Tidy instance. There are a couple of utility methods to test the number of warning of errors reported ( assertNoWarnings(), assertNoErrors(), assertWarnings(int expectedNumber), assertErrors(int expectedNumber) ) This is enough to test simple documents (documents you know must throw 1 or none errors). If you can't have a simple test and you need to test output in error log you can use the assertLogContains(String expectedString) method or directly check the out in the errorLog class instance (be careful with internationalization!).

Non-J Tidy tests

A big effort is being put in making JTidy working with the test cases supplied for the non java version of Tidy. All the tests from the tidy code base have been integrated in JTidy tests. Most of them can't work now, since newest features from Tidy have not been implemented yet in JTidy. Many other don't work also if they should: many bugs have been fixed in the original Tidy but not yet in JTidy, since the Tidy project was a lot more active than JTidy in the latest years.

If you can supply a patch to make a test case work as expected (as it work for Tidy) please do that! Patch should be submitted by non-developers attaching them to items in the sourceforge bug tracker. If your patch works and does not cause problems to other test cases you can be sure it will be applied in a short time.