摘要1:https://blog.csdn.net/yxxxiao/article/details/94591174#5.conftest.py%E7%9A%84%E4%BD%9C%E7%94%A8%E8%8C%83%E5%9B%B4
摘要2:https://www.jianshu.com/p/953a33005ab9
摘要3:https://blog.csdn.net/muyimo/article/details/84794796
摘要4(pytest插件):https://blog.csdn.net/shuyaoyao/article/details/104773101
pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点:
1、简单灵活,容易上手,文档丰富;2、支持参数化,可以细粒度地控制要测试的测试用例;3、能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests);4、pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等;5、测试用例的skip和xfail处理;6、可以很好的和CI工具结合,例如jenkins与安装其他的python软件无异,直接使用pip安装。
pip install -U pytest安装完成后,可以验证安装的版本:
pytest --version1)pytest
会查找当前目录及其子目录下以 test_*.py 或 *_test.py 文件,找到文件后,在文件中找到以 test 开头函数并执行
1)pytest somepath
会查找somepath目录下以 test_*.py 或 *_test.py 文件,找到文件后,在文件中找到以 test 开头函数并执行
2)pytest **.py
执行该文件中test开头的函数并执行
3)pytest **.py::test_func
执行指定测试文件下的测试函数
3)pytest **.py::classname::test_functionname
执行某个文件下某个类的某个方法
1)perferences ---->Tools -----> Python Integrated Tools ---->Default test runner设置为pytest 。之后直接执行测试用例即可
2)if __name__ =="__main__":
command_line = ["-s","./test_creative_create_card_pic_nobase.py","--alluredir=../result"]
pytest.main(command_line)
1、-K EXPRESSION
执行某个关键字的用例 用例要匹配给出的表达式;使用python的语法,匹配的范围是文件名、类名、函数名为变量,用and来区分
如下面一段测试用例(文件实现方式)
# content of test.py def test_two(): x = "hello" assert hasattr(x, 'check') def test_a(): assert 1 == 2运行pytest时带-k参数
# 执行当前文件名称中包含test_a字符的测试用例 pytest -k "test_a" test.py # 执行当前文件名称中不包含test_a字符的测试用例 pytest -k "not test_a" test.py # 执行当前文件名称中包含 test_a 字符或 test_two 字符的测试用例 pytest -k "test_a or test_two" test.py如下面一段测试用例(类实现方式)
# content of test.py class TestClass(object): def test_zne(self): x = "this" assert 'h' in x def test_two(self): x = "hello" assert hasattr(x, 'check') def test_a(self): assert 1==2运行pytest时带-k参数
# 测试类名包含 TestClass,并且该测试类中包含 test_a 将被跳过 pytest -k "test and TestClass and not test_a" test.py 或者 pytest -k "TestClass and not test_a" test.py # 测试类或函数包含 test_a 或 test_two 中的测试将被运行 pytest -k "test_a or test_two"如下面一段测试用例(类实现方式)
2、-x
遇到执行失败的用例或错误,停止测试
3、--maxfail=num
当错误个数到达给定个数,退出测试,这里就不列举实例了,结果与-x类似
4、-m MARKEXPR
只能运行有相应标识的测试用例,使用这个参数,测试用例要使用@pytest.mark.marker修饰
如下实例
# content of test.py class TestClass(object): def test_zne(self): x = "this" assert 'h' in x @pytest.mark.b def test_two(self): x = "hello" assert hasattr(x, 'check') @pytest.mark.a def test_a(self): assert 1 == 2teste_two使用了@pytest.mark.b来修饰,teste_a使用了@pytest.mark.a来修饰。
# 只执行a标识的用例 pytest –m a test.py # 如果要运行多个标识的话,用表达式,如下 注意,-m后面不能带''号(单引号),只能带""(双引号),不然识别不到 pytest -m "a or b" # 运行有 a 标识或 b 标识用例 pytest -m "a and not b" # 运行有 a 和没有 b 标识的用例5、 -v, --verbose
详细结果
6、-q, --quiet
极简结果显示,简化控制台的输出,可以看出输出信息和之前不添加-q不信息不一样, 下图中有两个..点代替了pass结果
7、-s
输入我们用例中的调式信息,比如print的打印信息等,我们在用例中加上一句 print(driver.title),我们再运行一下我们的用例看看,调试信息输出
8、-V
可以输出用例更加详细的执行信息,比如用例所在的文件及用例名称等
9、--junit-xml=path
输出xml文件格式,在与jenkins做集成时使用
10、 --result-log=path
将最后的结果保存到本地文件中