Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 631
Zack Avatar asked Sep 05 '25 01:09

Zack


1 Answers

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

like image 125
Crunchie Avatar answered Sep 06 '25 17:09

Crunchie