pythontestinginstallationnosetests

Setup method for nosetest. (Test class)


I am mocking out a database in some tests that I am doing. How would I create a setup method for the entire class, such that it runs each time an individual test within the class runs?

Example of what I am attempting to do.

from mocks import MockDB

class DBTests(unittest.TestCase):

    def setup(self):
        self.mock_db = MockDB()

    def test_one(self):
        #deal with self.mock_db

    def test_two(self):
        #deal with self.mock_db, as if nothing from test_one has happened

I'm assuming a teardown method would also be possible, but I can't find documentation that will do something like this.


Solution

  • If you are using Python unit test framework something like this is what you want:

    class Test(unittest.TestCase):
    
    
        def setUp(self):
            self.mock_db = MockDB()
    
        def tearDown(self):
            pass  # clean up 
    
        def test_1(self):
            pass  # test stuff
    

    Documentation