Best Python code snippet using tempest_python
06_pytest_user_fixtures.py
Source:06_pytest_user_fixtures.py
...6import pytest7 8# ÐÑоÑÑÐ°Ñ Ð¿Ð¾Ð»ÑзоваÑелÑÑÐºÐ°Ñ ÑикÑÑÑÑа - ÑÑо ÑÑнкÑÐ¸Ñ Ñ Ð´ÐµÐºÐ¾ÑаÑоÑом @pytest.fixture()9@pytest.fixture()10def resource_setup(request):11 print("ÐодгоÑовка ÑеÑÑÑÑов")1213 def resource_teardown(): # <- ФÑнкÑÐ¸Ñ ÑбÑоÑа окÑÑжениÑ14 print("ÐÑвобождение ÑеÑÑÑÑов")15 request.addfinalizer(resource_teardown) # <- Ðобавление ÑинализаÑоÑа1617 18# Ðо ÑмолÑаниÑ, scope = "function", autouse=False19@pytest.fixture(scope="function", autouse=True)20def another_resource_setup_with_autouse(request):21 print("another_resource_setup_with_autouse")22 def resource_teardown():23 print("another_resource_teardown_with_autouse")24 request.addfinalizer(resource_teardown)2526# ФикÑÑÑÑа Ð¼Ð¾Ð¶ÐµÑ Ð²Ð¾Ð·Ð²ÑаÑаÑÑ ÑÑо-Ñо в ÑеÑÑ (обÑекÑ/ÑеÑÑÑÑ)27@pytest.fixture(scope="module")28def resource_setup(request):29 print("\n >> 'ÐодклÑÑение' к ÐÐ")30 db = {"Red":1, "Blue":2, "Green":3}31 def resource_teardown():32 print("\n >> 'ÐÑклÑÑение' Ð¾Ñ ÐÐ")33 request.addfinalizer(resource_teardown)34 return db35 3637@pytest.yield_fixture()38def resource_setup_with_yield():39 print("resource_setup_with_yield")40 yield41 print("resource_teardown_with_yield")42
...
test_2.py
Source:test_2.py
1import pytest2def resource_teardown():3 print("\ndisconnect")4@pytest.fixture(scope='function') # scope - ÑÑовенÑ(Ð´Ð»Ñ Ñ-Ñии/ вÑеÑ
Ñ-Ñий в Ñайле)5def resource_setup(request):6 print("\nconnect to conn")7 db = {"Red": 1, "Blue": 2, "Green": 3}8 request.addfinalizer(resource_teardown) # добавиÑÑ Ð²Ñполнениие Ñ-Ñии поÑле вÑÐ¿Ð¾Ð»Ð½ÐµÐ½Ð¸Ñ ÑеÑÑа9 return db10def test_red(resource_setup):11 print(resource_setup) # Ñо ÑÑо возвÑаÑÐ°ÐµÑ Ñ-ÑÐ¸Ñ resource_setup12 assert resource_setup["Red"] == 113@pytest.mark.parametrize("x", [-1, 2])14@pytest.mark.parametrize("y", [-10, 11]) # паÑамеÑÑизÑÐµÑ Ð°ÑгÑменÑÑ ÑеÑÑ-ÑÑнкÑии15def test_cross_params(x, y):16 print(f"x: {x} y:{y}") # пÑи неÑколÑкиÑ
паÑамеÑÑизаÑиÑÑ
бÑÐ´ÐµÑ Ð´ÐµÐºÐ°ÑÑово пÑоизведение паÑамеÑÑов...
test_one.py
Source:test_one.py
1import pytest2@pytest.fixture(scope="module")3def resource_setup(request):4 print("\nconnect to db")5 db = {"Red": 1, "Blue": 2, "Green": 3}6 def resource_teardown():7 print("\ndisconnect")8 request.addfinalizer(resource_teardown)9 return db10def test_db(resource_setup):11 for k in resource_setup.keys():12 print("color {0} has id {1}".format(k, resource_setup[k]))13def test_red(resource_setup):14 assert resource_setup["Red"] == 115def test_blue(resource_setup):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!