介绍
官方文档:
https://docs.pytest.org/en/stable/
安装:pip install -U pytest
查看安装版本:pytest --version
pytest安装
① pip install pytest
升级: pip install -U pytest
② pycharm安装
两种运行方式
1.python解释器
2.pytest解释器
pytest -v 打印详细日志
pytest -k 指定某个测试用例
pytest的参数化
@pytest.mark.parametrize
@pytest.mark.parametrize('a,b',[ (1,2), (10,20), ('a','b') ]) def test_answer(a,b): assert inc(a) == b
import pytest def inc(x): return x + 1 @pytest.mark.parametrize('a,b',[ (1,2), (10,20), ('a','b') ]) def test_answer(a,b): assert inc(a) == b def test_answer1(): assert inc(4) == 5 if __name__ == '__main__': pytest.main(['test_sample.py::test_answer1','-v']) @pytest.fixture() def login(): print("登录操作") username = 'lili' return username class TestDemo: def test_a(self,login): print(f"a {login}") def test_b(self): print("b") def test_c(self,login): print(f"c {login}")