Tom Tom
Tom Tom

Reputation: 23

how to run the static method with each test

I have a function where I clean the database and reports. I would like to run this function every time I run tests (questions), regardless of whether I run one test (a method from the class) or the whole class.

My script is a static method

@staticmethod
def startScript():
     removeData()
     removeReport()

My example test:

test.py
class myTesting(unittest.TestCase):
def test_add_new_user(self, name, elem1, elem2):
code my test.

how to add a script to run it every time you run tests? Thank you for your help

Upvotes: 0

Views: 1684

Answers (1)

Avishay Cohen
Avishay Cohen

Reputation: 2208

add it as a fixture to your functions

https://docs.pytest.org/en/latest/fixture.html

@pytest.fixture
def startScript():
     removeData()
     removeReport()

or add it to your setup_method in class:

def setup_method(self, method):
    startScript();

a third way: add the static function to the conftest.py of the scope, this file should be in the same level as test.py :

@pytest.fixture(autouse=True, scope='function'):
def startScript():
    removeData()
    removeReport()

more info on conftest.py:

https://docs.pytest.org/en/2.7.3/plugins.html?highlight=re

Upvotes: 1

Related Questions