pytestテストフレームワークでのセットアップとtearDown-0



Setup Teardown Pytest Test Framework 0



最近、私はpytestにもっと興味があり、pytestのドキュメントを読んでいます クラシックなxunitスタイルのセットアップ 、ここに要約があります。コードを直接見てください。

# content of test_websites.py ''' Setup/teardown in pytest, see https://docs.pytest.org/en/3.5.1/xunit_setup.html Remarks: 1. The pairing function of setup/teardown can be called multiple times during the test process. 2. If the setup function fails or is skipped during execution, the corresponding tearDown function will not be called. ''' import pytest def setup_module(module): ''' This is a module level setup, it will be in this module (test_website.py) Before all tests are executed, they are called once. Note that it is directly defined as a function ''' in a module print() print('-------------- setup before module --------------') def teardown_module(module): ''' This is a module-level teardown, it will be in this module (test_website.py) After all tests are executed, they are called once. Note that it is directly defined as a function ''' in a module print('-------------- teardown after module --------------') class TestBaidu(object): def test_login(self): print('test baidu login function') assert True == True class TestSohu(object): @classmethod def setup_class(cls): ''' This is a class level setup function, it will be in this test class TestSohu Before all tests are executed, they are called once. Note that it is a @classmethod ''' print('------ setup before class TestSohu ------') @classmethod def teardown_class(cls): ''' This is a class-level teardown function, it will be tested in this After all tests in the class TestSohu are executed, they are called once. Note that it is a @classmethod ''' print('------ teardown after class TestSohu ------') def setup_method(self, method): ''' This is a method level setup function, it will be tested in this In the TestSohu class, before each test is executed, it is called once. ''' print('--- setup before each method ---') def teardown_method(self, method): ''' This is a method level teardown function, it will be tested in this In the TestSohu class, after each test is executed, it is called once. ''' print('--- teardown after each method ---') def test_login(self): print('sohu login') assert True == True def test_logout(self): print('sohu logout') pytest.skip()

pytestでセットアップ/ティアダウンを実装するためのより推奨される方法は、pytest.fixture機能を使用することです。上記の従来のセットアップ/ティアダウンは、引き続きpytestでサポートされます。次のブログでは、pytest.fixtureを使用してセットアップ/ティアダウンを実装する方法を要約します。