5 PyTest Fixture Scopes (2 Min) You Should Know In Python

5 PyTest Fixture Scopes (2 Min) You Should Know In Python

Gokce DB

2 года назад

1,410 Просмотров

In this tutorial, you'll learn 5 Pytest Fixture Scopes in Python.


Facebook: https://www.facebook.com/GokceDBsql

Connect MySQL In Python: https://www.youtube.com/watch?v=0qBctY82et0

Video Transcript:

Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn five fixture scopes that you can use in the PI test. let's start by looking at the test directory.

I have two test files and a conf test.py file. Conf test.py file contains a fixture called rad underscore config that is being used in the two test files in the cache sales test file. I have two test functions and I'm passing the read underscore config fixture as the first argument.

In the customer test file, I have three test functions. The first function is a standalone function and the other two test functions are defined within a test class. I'm also using a PI test dot mark.

Dot uses a fixtures decorator to pass the read underscore config fixture to the test class. A PI test fixture can have five scope values function, session, module, package, and class. Let's test them out one by one.

When I specify scope equal to function for the read underscore config fixture it got executed for every test function. By default, the scope is set to function so if I remove scope equal to function and rerun the test we should see the same output specifying scope equal to a session will only execute the read underscore config fixture once for the whole run. Changing the scope to module will make the read underscore config fixture execute just once per test file.

Next, if I change the scope to package the read underscore config fixture will only get executed once for the whole test package. Finally, if I change the scope to class the read underscores config fixture will get executed for every test function. However, it will only get executed once for the test class.

There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next time.


# function, session, module, package, class
@pytest.fixture(scope="class")
def read_config():
parser = ConfigParser()
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
parser.read(root_dir + '/resources/queries.txt')
print("############## Inside PyTest Fixture: read_config() ##############")
return parser

Тэги:

#pytest_fixtures #pytest_tutorial #python_tutorial #what_is_pytest #python_testing #python_pytest #pytest_example #pytest_fixtures_tutorial #test_automation #pytest_framework_in_python #how_to_create_a_test_in_pytest #pytest_setup #unit_testing_with_pytest #how_to_create_multiple_tests #using_fixtures_in_pytest #pytest_fixture_scope #pytest_fixture_conftest #pytest_fixture_scope_explained #pytest_fixtures_explained #pytest_fixtures_youtube #pytest_tutorial_youtube
Ссылки и html тэги не поддерживаются


Комментарии: