Changes in Nette Tester 1.2.0

11 years ago by Miloslav Hůla  

Nette Tester is a PHP testing tool. It has been four months since the Tester 1.0.0 has been released, and it is a few days since the version 1.2.0 is out. I would like to describe the changes in here.

If you are interested in how well your tests cover your code, the new Tester's options --coverage cc.html and --coverage-src path should be useful. The first one enables the collection of coverage details and outputs it in cc.html. You will get a well-arranged HTML report. The second option reduces the path to the analyzed source files (instead of passing the directory, you can pass a single file as a path since version 1.2.0).

Since running the tests in parallel has been shown as non-problematic, the tests will now run in 33 parallel threads by default. That's all.

I will describe the next change via an example, imagine this simple test case:

use Tester\Assert;

class MyTest extends Tester\TestCase
{
	public function testSomething()
	{
		Assert::fail('I have no reason to be a success.');
	}
}

One would expect the test to fail, but unfortunately, the test would succeed in Tester version 1.0. It happens because (new MyTest)->run() is missing. Since version 1.1.0, Tester checks for the number of executed assertions, and reports a failure, if there are none. If you don't like this behavior, you can disable it via Tester\Environment::$checkAssertions = FALSE.

Other changes are focused on the Tester\Assert class. Assert::isEqual() now supports comparing of recursive structures. New assertion Assert::isNan() has been added, because it is hard to compare PHP's NAN. If you want to test the NAN value, you should definitely use this assertion. The last one is a new Assert:count() method. It may not look very useful, because Assert::same( 1, count($var) ) should be the same, shouldn't it? No, because of the count() assertion checks that $var is an array, or that it implements the Countable interface. It prevents mistakes such as count("").

Option -log has been renamed to -l (or --log). The only reason for this is to keep consistency across the options. The old name still works without warning, but it may be removed in a future version. Please, update your scripts.

A new option --stop-on-fail has been added. It stops the execution after the first test failure. It brings some new possibilities for your workflow. Try to combine it with --watch.

A completely new Tester\FileMock class has been introduced. It emulates real files in memory, so you can easily test code which contains functions like fopen(), file_get_contentst() or parse_ini_file(). You can find an example in the documentation.

The Tester\DomQuery class now supports HTML5 tags properly. You would get PHP warnings before, even if you would have loaded a valid HTML5 document. The only possible workarounds were either to use a shut-up operator @, or manually handle LibXML errors. You are now shielded from this odd PHP behavior.

If you are using @dataProvider in the test file doc block, a new method Tester\Environment::loadData() is available for you. It returns arguments for the current data provider entry.

If you are using @dataProvider in the test case method's doc blocks, you can use default values of the target method's arguments. For example, the following code will work:

# example.ini

[one]
method = 'GET'

[two]
method = 'POST'
paylod = '{.....}'
/** @dataProvider example.ini */
public function testApi($method, $paylod = NULL)
{
}

…moreover you don't have to worry about the named arguments' order. They will be bound by their names.

The last significant change is the possibility to combine the @dataProvider, @testCase and @multiple together in one test. It brings up crazy combinations such as run every test case method for every data provider entry, and run it 20 times.

That's all for now. You can find other details in the documentation, which is up to date.

One more thing: The Tester is available as a single PHAR file. You can download it from the GitHub release page.

Comments

  1. For code coverage, only HTML format is available? Asking because we could add it to NetBeans but we strongly prefer XML (since it is usually more stable format).

    Thanks.

    11 years ago · replied [2] Milo
  2. #1 tmysik Yes. If you pass file path with .html or .htm extension, you get HTML. Otherwise you get a raw internal serialized format. Would you open an issue just to keep it in mind, please?

    11 years ago

Sign in to submit a comment