def fixture( # noqa: F811 fixture_function: Optional[_FixtureFunction] = None, *, scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = "function", params: Optional[Iterable[object]] = None, autouse: bool = False, ids: Optional[ Union[ Iterable[Union[None, str, float, int, bool]], Callable[[Any], Optional[object]], ] ] = None, name: Optional[str] = None ) -> Union[FixtureFunctionMarker, _FixtureFunction]: fixture_marker = FixtureFunctionMarker( scope=scope, params=params, autouse=autouse, ids=ids, name=name, ) # Direct decoration. if fixture_function: return fixture_marker(fixture_function) return fixture_marker |
@pytest.fixture() def login(): print('登录操作') yield print('退出登录') |
import pytest @pytest.fixture() def login(): print('登录操作') yield print('退出登录') class Test_Login(): def test_01(self, login): print('需要用到登录!') def test_02(self): print('不需要登录!') def test_03(self, login): print('这里需要用到登录!') if __name__ == '__main__': pytest.main(['-vs']) |
import pytest @pytest.fixture() def login(): print('登录操作') yield print('退出登录!') class Test_Login(): def test_01(self, login): print('需要用到登录!') assert 1==1 def test_02(self): print('不需要登录!') def test_03(self, login): print('这里需要用到登录!') assert 1==2 if __name__ == '__main__': pytest.main(['-vs'] |
import pytest @pytest.fixture() def login(): print('登录操作') yield print('退出登录!') @pytest.fixture() def log(): print('打开日志功能!') yield print('关闭日志功能!') class Test_Login(): def test_01(self, login, log): print('需要用到登录!') def test_02(self): print('不需要登录!') def test_03(self, log, login): print('这里需要用到登录!') if __name__ == '__main__': pytest.main(['-vs']) |
import pytest @pytest.fixture() def login(): print('\n登录操作') yield print('\n退出登录!') @pytest.fixture() def log(login): print('\n打开日志功能!') yield print('\n关闭日志功能!') class Test_Login(): def test_01(self, log): print('\n需要用到登录!') def test_02(self): print('\n不需要登录!') def test_03(self, log): print('\n这里需要用到登录!') if __name__ == '__main__': pytest.main(['-vs']) |
import pytest @pytest.fixture(name='anjing') def login(): print('\n登录操作') yield print('\n退出登录!') class Test_Login(): def test_01(self, anjing): print('---用例01---') def test_02(self): print('---用例02---') def test_03(self, anjing): print('---用例03---') if __name__ == '__main__': pytest.main(['-vs']) |
_Scope = Literal["session", "package", "module", "class", "function"] |
import pytest @pytest.fixture(scope='session') def fix_session(): print('\n这是属于session') yield print('\nsession') @pytest.fixture(scope='class') def fix_class(): print('\n这是属于class') yield print('\nsession') @pytest.fixture(scope='module') def fix_module(): print('\n这是属于module') yield print('\nmodule') @pytest.fixture(scope='function') def fix_function(): print('\n这是属于function') yield print('\nfunction') class Test_01: def test_01(self, fix_function, fix_class,fix_module,fix_session): print('----用例01---') if __name__ == '__main__': pytest.main(['-vs']) |
import pytest @pytest.fixture(autouse=True) def login(): print('\n登录操作') yield print('\n退出登录!') class Test_Login(): def test_01(self): print('---用例01---') def test_02(self): print('---用例02---') def test_03(self): print('---用例03---') if __name__ == '__main__': pytest.main(['-vs']) |
import pytest data = ['anjing', 'test', 'admin'] @pytest.fixture(params=data) def login(request): print('登录功能') yield request.param print('退出登录') class Test_01: def test_01(self, login): print('---用例01---') print('登录的用户名:%s' % login) def test_02(self): print('---用例02---') if __name__ == '__main__': pytest.main(['-vs']) |
import pytest data = ['anjing', 'test', 'admin'] @pytest.fixture(params=data, ids=['user=anjing','user=test','user=admin']) def login(request): print('登录功能') yield request.param print('退出登录') class Test_01: def test_01(self, login): print('---用例01---') print('登录的用户名:%s' % login) def test_02(self): print('---用例02---') if __name__ == '__main__': pytest.main(['-vs']) |
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |