1 Fixture Concept
Fixture is the concept in testing:
- Fixture refers to data and conditions that are dependent on the test, etc.
- Python's UnitTest library provides some support for fixture
- Each testcase should be responsible for the creation of resources in setup, such as
Class Mytestcase (UnitTest. TestCase): def my_fixture_setup (self): pass def setup (self): super (Mytestcase, self). Setup () Self.my_fixture_setup ()
- Each testcase should create a new function that is responsible for the destruction of resources. and add this new function to TestCase's cleanup list.
Class Mytestcase (UnitTest. TestCase): def my_fixture_cleanup (self): print ("++++ my_cleanup") def setUp (self): super ( Mytestcase, self). SetUp () self.addcleanup (Self.my_fixture_cleanup)
The following is the complete code
From __future__ import Print_functionimport unittest class Mytestcase (unittest. TestCase): def my_fixture_setup (self): pass def my_fixture_cleanup (self): print ("++++ my_cleanup" ) def setup (self): super (Mytestcase, self). Setup () self.my_fixture_setup () Self.addcleanup ( Self.my_fixture_cleanup) def tearDown (self): super (Mytestcase, self). TearDown () print ("++++ TearDown ") def My_cleanup (self): print (" ++++ my_cleanup ") def test_case_1 (self): print (" ++++ test Case 1 ")
The fixtures package fixtures (complex number) in Python 2 is a package in Python that provides toolkits for quick creation/destruction of fixture
Https://pypi.python.org/pypi/fixtures
- The Test case needs to be derived from TestTools. TestCase class
- If you need to customize fixtures, you need to derive from fixtures. Fixture class, and overwrite the Setup/cleanup method of the parent class
An example of a custom fixtures class is as follows
From __future__ import print_functionimport fixturesimport testtoolsclass myfixture (fixtures. Fixture): def setup (self): super (Myfixture,self). Setup () self.frobnozzle = print ("++++ Myfixture.setup () ") def cleanUp (self): super (Myfixture,self). CleanUp () print (" ++++ myfixture.cleanup () ") print () class Mytestcase (TestTools. TestCase): def setup (self): super (Mytestcase, self). Setup () self.my_fixture = Self.usefixture ( Myfixture ()) print ("++++ setUp") def TearDown (self): super (Mytestcase, self). TearDown () Print ("++++ TearDown") def test_case_1 (self): self.assertequal (self.my_fixture.frobnozzle) print ("++++ test Case 1")
Fixture and fixtures in Python