APP UI 自动化注意事项

多CPU运行Case  -pytest-xdist

当cases量很多时,运行时间也会变的很长,如果想缩短脚本运行的时长,就可以用多进程来运行。插件安装如下:

pip install -U pytest-xdist -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

自定义进程数运行测试用例: -n=auto(自动检测到系统的CPU核数)    -n=N(指定运行的CPU核数为N)

......if __name__ == "__main__":

    pytest.main(["-n=N"])

pytest-xdist默认是无序执行的,可以通过参数--dist=loadscope来控制顺序:

1. 将同一个模块.py下的函数和同一个测试类class下的方法来分组,然后将每个测试组发给可以执行的worker,确保同一个组的测试用例在同一个进程中执行

2. 目前无法自定义分组,按类class分组优先于按模块module分组

Case失败重跑 - pytest-rerunfailures

在做UI测试时, 短时的网络波动会导致case运行失败, 这并非是我们期望的结果, 此时就可通过重试运行cases的方式来解决问题

插件安装如下:

pip install -U pytest-rerunfailures -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

自定义重跑次数N与重跑延迟时间M/秒

......if __name__ == "__main__":

    pytest.main(["--reruns=N", "--reruns-delay=M"])

注意事项:如果指定了用例的重新运行次数,则在命令行添加--reruns对这些用例是不会生效的 

兼容性问题:

1. 不可以和fixture装饰器一起使用: @pytest.fixture() 

2. 该插件与pytest-xdist的 --looponfail 标志不兼容

3. 该插件与核心--pdb标志不兼容

HTML测试报告 - pytest-html

pytest-HTML是一个pytest用于生成XML/HTML测试报告的插件

插件安装如下:

pip install pytest-html -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

指定HTML测试报告文件路径

......if __name__ == "__main__":

    pytest.main(["--html=report/report.html"])

上面命令生成的报告css是独立的, 分享报告的时候样式会丢失, 为了更好的分享发邮件展示报告, 可以把css样式合并到HTML里

pytest --html=report/report.html --self-contained-html

多重校验 - pytest-assume

Pytest中可用assert断言, 但一个失败后面的断言将不再执行;pytest-assume可以实现对多个断言多重校验的

插件安装如下:

pip install pytest-assume -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

代码示例:

import pytestdeftest_01(): print("=======> start test_01")

    pytest.assume(1 == 2)              # 多重断言    pytest.assume(2 == 2)

    pytest.assume(3 == 2)

    print("=======> end test_01")if __name__ == "__main__":

    pytest.main(["-s", "test_pytest_assume.py"])

重复执行测试 - pytest-repeat

测试过程中会出现一些偶现的bug, 对于这种问题我们会针对此用例反复执行多次, 最终复现出问题来; pytest-repeat插件就能起到这个作用: 重复执行测试用例

参数--count=N表示执行N次

注:将pytest的 -x 选项与pytest-repeat结合使用,以强制测试运行程序在第一次失败时停止

插件安装如下:

pip install pytest-repeat -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

代码示例:

自定义用例执行顺序 - pytest-ordering

pytest默认按字母顺序去执行的(小写英文--->大写英文--->0-9数字)

用例之间的顺序是文件之间按照ASCLL码排序,文件内的用例按照从上往下执行

setup_module->setup_claas->setup_function->testcase->teardown_function->teardown_claas->teardown_module

但可以通过第三方插件pytest-ordering实现自定义用例执行顺序

注:一旦设置了自定义的执行顺序,就必须得延伸@pytest.mark.run(order=1)里面得order字段

插件安装如下:

pip install pytest-ordering -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

示例代码如下:

若是未通过@pytest.mark.run(order=n)指定执行顺序则执行顺序是test_01->test_02->test_03

如上通过@pytest.mark.run(order=n)指定执行顺序(按test_02->test_03->test_01),运行结果如下:

方式一

1. 第一个执行:@pytest.mark.first或者@pytest.mark.run('first')

2. 第二个执行:@pytest.mark.second或者@pytest.mark.run('second')

3. 倒数第二个执行:@pytest.mark.second_to_last 或者@pytest.mark.run('second_to_last')

4. 最后一个执行:@pytest.mark.last 或者@pytest.mark.run('last')

方式二

1. 第一个执行:@pytest.mark.run(order=1)

2. 第二个执行:@pytest.mark.run(order=2)

3. 倒数第二个执行:@pytest.mark.run(order=-2)

4. 最后一个执行:@pytest.mark.run(order=-1)

你可能感兴趣的:(APP UI 自动化注意事项)