Running the tests¶
Once you have written some tests using pyqcy, you would most likely want run them.
If you already have a test suite of different kinds of tests for your projects (typically at least unit tests), you probably want to integrate pyqcy properties into that.
Alternatively, properties can be also verified using a built-in, standalone test runner.
pyqcy includes a readily available test runner which can be used to run verification
tests for all properties defined within given module. For it to work, you just need
to include a traditional
if __name__ == '__main__': boilerplate which calls
from pyqcy import * # ... define test properties here ... if __name __ == '__main__': main()
This default test runner will go over all properties defined within this module, as well as all modules it imports, and execute tests for them. It is intentionally similar in usage to standard unittest.main and shares many parameters with the unittest runner (to the extent it makes sense for pyqcy tests, of course).
main(module='__main__', exit=True, verbosity=2, failfast=False)¶
Built-in test runner for properties.
When called, it will look for all properties (i.e. functions with
qc()decorator) and run checks on them.
Arguments are intended to mimic those from
unittest.main(). Return value is the total number of properties checked, provided
Falseand program doesn’t terminate.
Integration with testing frameworks¶
If you are already using a unit testing framework, you can easily integrate pyqcy property tests into it.
For this, there is a
TestCase class which is a descendant of the standard
unittest.TestCase. Any test cases built upon it will be gathered and ran
by pretty much any testing framework - be it
unittest itself, nose, py.test, etc.
Therefore all we need to do is to put out properties inside a
from pyqcy import * class Arithmetic(TestCase): @qc def addition_on_ints(x=int, y=int): assert isinstance(x + y, int) @qc def subtraction_on_ints(x=int, y=int): assert isinstance(x - y, int)
There is no need to rename the properties to start with
test_ but we should retain
qc() decorator. We also don’t need to include any other methods that would
explicitly run tests for our properties, as the base
will take care of it automatically.
unittest test case for pyqcy properties.
Properties defined here within subclasses of
TestCasewill be verified automatically as a part of standard unittest run. To define them, use the typical syntax with
class Sorting(TestCase): '''Properties that must hold for a sorting.''' @qc def sort_preserves_length( l=list_(of=int, max_length=128) ): assert len(l) == len(list(sorted(l))) @qc def sort_finds_minimum( l=list_(of=int, min_length=1, max_length=128) ): assert min(l) == list(sorted(l))
TestCaseitself is a subclass of standard
unittest.TestCase, it will be discovered by
unittest.main(), nosetests or similar testing utilities.