Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytest init setup for few modules

Say i have next tests structure:

test/
  module1/   
    test1.py
  module2/
    test2.py
  module3/
    test3.py

How can i setup some method to be called only once before all this tests?

like image 966
Ostap Maliuvanchuk Avatar asked Oct 07 '13 12:10

Ostap Maliuvanchuk


2 Answers

You can use autouse fixtures:

# content of test/conftest.py 

import pytest
@pytest.fixture(scope="session", autouse=True)
def execute_before_any_test():
    # your setup code goes here, executed ahead of first test

See pytest fixture docs for more info.

like image 64
hpk42 Avatar answered Nov 11 '22 13:11

hpk42


If you mean only once in each run of the test suit, then setup and teardown are what you are looking for.

def setup_module(module):
    print ("This will at start of module")

def teardown_module(module):
    print ("This will run at end of module")

Similarly you can have setup_function and teardown_function which will run at start and end of each test function repetitively. You can also add setup and teardown member function in test classes to run at start and end of the test class run.

like image 1
Amit K. Avatar answered Nov 11 '22 12:11

Amit K.