UnitPy
UnitPy is a toolbox with python annotations similar to the java unit test annotations, using @Test
@Before
@After
@RunWith
etc. UnitPy also provides a tool to discover and run tests.
UnitPy has a number of improvements over the standard method using Testcase:
- the test class must not extend
unittest.TestCase
- the test class constructor does not need modifications (TestCase requires the name of the test function to be passed as additional argument).
- The test methods are not required to contain "test"
- Every method annotated with
@Test
is a test - The method annotated with
@Before
is run before every test. - The method annotated with
@After
is run after every test. - Every test class must be annotated with
@RunWith
This enables to use the test class also as normal class.
The @RunWith
annotation is to be placed right before the class definition.
@RunWith
allows the class to be run in different "styles", depending on the argument provided to @RunWith
.
annotation | |
---|---|
@RunWith(JUnit4ClassRunner) | normal test |
@RunWith(Parameterized) | parametric test |
JUnit4ClassRunner
This runs all methods annotated with @Test inside the class.
If there is a method annotated @Before
, this is called first.
Then the test method is called (with no arguments)
Finally, if there is a method annotated @After
, that method is called
Parameterized
The class must contain a static method annotated with @Parameters
.
This annotation indicates that the method takes no parameters
and that it returns list of lists. Every inner list
is to be used to call the constructor, so the number
of arguments must match the __init__
function parameters.
For every method M inside the class that is annotated with @Test, the following now happens:
- create an instance of the test class, with the next inner list arguments
- If there is a method annotated @Before, this is called.
- execute the test method M
- if there is a method annotated @After, that method is called