# PyTest тип мануал #### Для включения работы print внутри тестов необходимо добавить флаг -s ```python= import pytest from time import time start: float # тестирование в виде фукнций def setup(): global start start = time() print("\nbasic setup into module") def teardown(): global start print("Время выполнение теста: ", time() - start) print("\nbasic teardown into module") def setup_module(module): print("\nmodule setup") def teardown_module(module): print("\nmodule teardown") def setup_function(function): print("\nfunction setup") def teardown_function(function): print("\nfunction teardown") def test_numbers_3_4(): print("\ntest 3*4") assert 3 * 4 == 12 def test_strings_a_3(): print("\ntest a*3") assert 'a' * 3 == 'aaa' # тестирование в виде класса class TestUM: def setup(self): print("\nbasic setup into class") def teardown(self): print("\nbasic teardown into class") def setup_class(cls): print("\nclass setup") def teardown_class(cls): print("\nclass teardown") def setup_method(self, method): print("\nmethod setup") def teardown_method(self, method): print("\nmethod teardown") def test_numbers_5_6(self): print("\ntest 5*6") assert 5 * 6 == 30 ``` Output: ```bash= Testing started at 12:15 ... C:\Users\rcsko\AppData\Local\Programs\Python\Python38-32\python.exe "C:\Program Files\JetBrains\PyCharm 2019.3.3\plugins\python\helpers\pycharm\_jb_pytest_runner.py" --path E:/DEV/python/sandbox/tests/test_classes.py -- -s Launching pytest with arguments -s E:/DEV/python/sandbox/tests/test_classes.py in E:\DEV\python\sandbox\tests ============================= test session starts ============================= platform win32 -- Python 3.8.3, pytest-7.2.1, pluggy-1.0.0 -- C:\Users\rcsko\AppData\Local\Programs\Python\Python38-32\python.exe cachedir: .pytest_cache rootdir: E:\DEV\python\sandbox\tests collecting ... collected 3 items test_classes.py::test_numbers_3_4 module setup function setup test 3*4 PASSED function teardown test_classes.py::test_strings_a_3 function setup test a*3 PASSED function teardown test_classes.py::TestUM::test_numbers_5_6 class setup method setup test 5*6 PASSED method teardown class teardown module teardown ============================== 3 passed in 0.01s ============================== Process finished with exit code 0 ``` ### Фикстуры Фикстуры набор методов расширяющих окружение PyTest (только моя интерпритация) ```python= import pytest @pytest.fixture() def resource_setup(request): print("\nresource_setup") def resource_teardown(): print("\nresource_teardown") request.addfinalizer(resource_teardown) def test_1_that_needs_resource(resource_setup): print("\ntest_1_that_needs_resource") def test_2_that_does_not(): print("\ntest_2_that_does_not") def test_3_that_does_again(resource_setup): print("\ntest_3_that_does_again") ``` output: ```bash! Testing started at 12:22 ... C:\Users\rcsko\AppData\Local\Programs\Python\Python38-32\python.exe "C:\Program Files\JetBrains\PyCharm 2019.3.3\plugins\python\helpers\pycharm\_jb_pytest_runner.py" --path E:/DEV/python/sandbox/tests/test_fixture.py Launching pytest with arguments E:/DEV/python/sandbox/tests/test_fixture.py in E:\DEV\python\sandbox\tests ============================= test session starts ============================= platform win32 -- Python 3.8.3, pytest-7.2.1, pluggy-1.0.0 -- C:\Users\rcsko\AppData\Local\Programs\Python\Python38-32\python.exe cachedir: .pytest_cache rootdir: E:\DEV\python\sandbox\tests collecting ... collected 3 items test_fixture.py::test_1_that_needs_resource resource_setup PASSED [ 33%] test_1_that_needs_resource resource_teardown test_fixture.py::test_2_that_does_not PASSED [ 66%] test_2_that_does_not test_fixture.py::test_3_that_does_again resource_setup PASSED [100%] test_3_that_does_again resource_teardown ============================== 3 passed in 0.02s ============================== Process finished with exit code 0 ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up