pytest特性
1. 很多第三方扩展插件可以直接使用,pytest-selenium, pytest-xdist, pytest-html...
2. 装饰器fixture:可以将函数作为参数使用
3. 多种方法实现参数化
4. 装饰器mark: 测试case分类
5. conftest:数据共享
问题
1. 使用pycharm,test文件和page文件分别在不同的目录下(testCases-test_sanity.py, pageObjects-hote_search_results_page.py),test文件引用page文件import pageObjects.hotel_search_Results_page.py,运行pytest的时候总是提示找不到pageObjects
- 方法一,在import之前添加两句#
import sys
sys.path.append("E:python_expedia") //项目根路径
- 方法二,运行执行case的命令前,先运行set PYTHONPATH=E:python_expedia
总结
pytest测试数据参数化的方法
1. 使用@pytest.fixture(), 测试数据与测试case在同一个文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# practice_is_leap_year1.py def is_leap_year(year): if isinstance(year, int) is not True: raise TypeError("year is not int") elif year == 0: raise ValueError("year shouldn't be 0") elif abs(year) != year: raise ValueError("year should be positiye number") elif(year%4 == 0 and year%100 !=0 or year%400 ==0): print("%d is leaf year" % year) return True else: return False
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# practice_is_leap_year2.py import pytest import practice_is_leap_year1 is_leap = [4, 40, 400, 800, 1996, 2996] is_not_leap = [1, 100, 500, 1000, 1999, 3000] is_valueerror = [0, -4, -100] is_typeerror = ['-4', 'ins'] @pytest.fixture(params=is_leap) def is_leap(request): return request.param @pytest.fixture(params=is_typeerror) def is_typeerror(request): return request.param def test_is_leap(is_leap): assert practice_is_leap_year1.is_leap_year(is_leap) == True def test_is_typeerror(is_typeerror): with pytest.raises(TypeError): practice_is_leap_year1.is_leap_year(is_typeerror)
2. 使用conftest.py文件,是测试数据与测试case分离
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# conftest.py is_leap = [4, 40, 400, 800, 1996, 2996] is_not_leap = [1, 100, 500, 1000, 1999, 3000] is_valueerror = [0, -4, -100] is_typeerror = ['-4', 'ins'] @pytest.fixture(params=is_leap) def is_leap_conftest(request): return request.param @pytest.fixture(params=is_typeerror) def is_typeerror_conftest(request): return request.param
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# practice_is_leap_year3.py import pytest import practice_is_leap_year1 class TestData: def test_is_leap(self, is_leap_conftest): assert practice_is_leap_year1.is_leap_year(is_leap_conftest) == True def test_is_typeerror(self, is_typeerror_conftest): with pytest.raises(TypeError): practice_is_leap_year1.is_leap_year(is_typeerror_conftest)
3. 使用@pytest.mark.parametrize(),可以让同一条case用不同的测试数据跑多次
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# practice_is_leap_year4.py import pytest import practice_is_leap_year1 @pytest.mark.parametrize('year, expected', ([1, False], [1996, True])) def test_is_leap(self, year, expected): assert practice_is_leap_year1.is_leap_year(year) == expected
pytest并行运行测试
使用插件pytest-xdist
1. pip install pytest-xdist
2. 执行case时命令行参数:
- -n 并行数量
pytest生成html测试报告
使用插件pytest-html
case报错的时候自带网页截屏
1. pip install pytest-html
2. 执行case时命令行参数 pytest --html=report.html (=后面可以指定具体报告生成路径)
3. 上面生成的报告,css是独立的,分享报告的时候样式会丢失,为了更好的分享发邮件展示报告,可以把css样式合并到html里 pytest --html=report.html --self-contained-html
pytest测试case失败重复执行
使用插件pytest-rerunfailures: pip install pytest-rerunfailures
1. running过程中:rerun所有的失败的case,执行case时命令行参数 pytest --reruns 2
2. running过程中:rerun单个case,case方法添加@pytest.mark.flaky(reruns=2)
3. running结束后,rerun所有刚刚失败的case "--lf"
pytest自定义case执行顺序
note: 一般情况下,case都是按照文件中的顺序执行的,但是有发现一种情况不会按照顺序执行:比如第1,3,4条case都有使用同一个conftest中的set up,但是第2个没用,那第二个case会在最后执行
使用插件pytest-ordering: pip install pytest-ordering
@pytest.mark.run(order=6)
pytest获取自定义的命令行参数
使用pytest内置的fixture:pytestconfig
# conffigtest.py
# pytest hook in configtest.py to add custom command line argument(固定写法) def pytest_addoption(parser): parser.addoption("--myopt", action="store_true", help="some boolean option") parser.addoption("--foo", action="store", default="bar", help="fop:bar or baz") parser.addoption("--nice", action="store_true", help="nice: turn failures into opportunities") # actions 有一组固定的值可供选择,默认是’store ‘,表示将命令行参数值保存在 options 对象里,可以获取参数的值并使用 # store 也有其它的两种形式: store_true 和 store_false ,用于处理带命令行参数后面不带值的情况。如果是store_true则赋值为true,如果是store_false则赋值为false
# test_command_line_params.py def test_option(pytestconfig): print('"foo" set to:', pytestconfig.getoption('foo')) print('"myopt" set to', pytestconfig.getoption('myopt'))
命令行输入:pytest -s --myopt --foo=baz d_pytest_fixture_pytestconfig.py
输出: "foo" set to: baz "myopt" set to true