python unittest lib - Serbipunk/notes GitHub Wiki
https://docs.python.org/2.7/library/unittest.html
https://docs.python.org/3/library/unittest.html
inspired by JUnit
common place:
- test automation
- sharing of setup
- shutdown code for tests ?
- independence of the tests from the reporting framework
basic concept
test fixture
represents the preparation needed to perform one or more tests, and any associate cleanup actions.
test case
literal meaning. unittest
provides a base class. TestCase
which may be used to create new test cases.
TestCase
, the setUp() and tearDown() methods can be overridden to provide initialization and cleanup for the fixture
test suite
A test suite is a collection of test cases
TestSuite
class. This class allows individual tests and test suites to be aggregated
test runner
a component which orchestrates the execution of tests, and provides the outcome to the user
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.
basic example
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()