Pre
- What type of test are you going to write? e.g. Unit tests, functional tests …
- What do you test against? In other words, how do you tell a function works well? e.g. Functional testing is against the functional requirements/specifications
- Do you make any assumptions while development? If yes, each assumption should have a test such that if the assumption breaks, the test will also break (to raise people’s attention).
Framework
? Why framework
– better reporting
– sharing of setup and shutdown code for tests
– aggregation of tests into collections
? Basic component
1 TestRunner run (TestSuite / TestCase)
1 TestSuite n TestCase
1 TestCase n TestMethod
? How does a good testing framework look like
– appropriate abstraction layers
– meaningful logging and reporting structure
– easily maintainable and extendable
– retry mechanism to rerun failed tests
Python unittest
- A testcase is created by subclassing unittest.TestCase
- N test methods belong to 1 test case. A test mehod name should start with the letters test. That informs the test runner about which methods represent tests.
- To run the tests:
- unittest.main() provides a command-line interface to the test script
- code below can make tests ran by interpreter or another script
suite = unittest.TestLoader().loadTestsFromTestCase(testCaseClass) unittest.TextTestRunner(verbosity=2).run(suite)
classunittest.TextTestRunner
: A basic test runner implementation which prints results on standard error
A test runner is an object that provides a single method,run()
, which accepts a TestCase or TestSuite object as a parameter, and returns a result object.