Fixture and fixtures in Python

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.