pytest使用总结笔记

 

简介

pytest是python的一种单元测试框架,与python自带的unittest测试框架相似,可是比unittest框架使用起来更简洁,效率更高。而且pytest兼容unittest的用例,支持的插件也更多html

安装java

pip install pytest

简单上手,建立个test_sample.py文件node

def func(x): return x + 1

def test_answer(): assert func(3) == 5

运行测试,直接在当前文件夹运行pytestpython

collected 1 item test_sample.py F [100%] ================================= FAILURES =================================
_______________________________ test_answer ________________________________

    def test_answer(): >       assert func(3) == 5 E assert 4 == 5 E +  where 4 = func(3) test_sample.py:6: AssertionError ============================ 1 failed in 0.12s =============================

pytest运行规则:查找当前目录及其子目录下以test_*.py或*_test.py文件,找到文件后,在文件中找到以test开头函数并执行。git

以类来封装用例github

# content of test_class.py
class TestClass: def test_one(self): x = "this"
        assert "h" in x def test_two(self): x = "hello"
        assert hasattr(x, "check")

运行能够使用pytest [file_path]指定文件,-q是静默模式,不会打印用例输出windows

$ pytest -q test_class.py .F [100%] ================================= FAILURES =================================
____________________________ TestClass.test_two ____________________________ self = <test_class.TestClass object at 0xdeadbeef>

    def test_two(self): x = "hello"
>       assert hasattr(x, "check") E AssertionError: assert False E +  where False = hasattr('hello', 'check') test_class.py:8: AssertionError 1 failed, 1 passed in 0.12s

用例设计原则session

  • 文件名以test_*.py文件和*_test.py
  • 以test_开头的函数
  • 以Test开头的类
  • 以test_开头的方法
  • 全部的包pakege必需要有__init__.py文件

 

执行用例

1.执行某个目录下全部的用例oracle

pytest 文件名/

2.执行某一个py文件下用例框架

pytest 脚本名称.py

3.-k 按关键字匹配

pytest -k "MyClass and not method"

这将运行包含与给定字符串表达式匹配的名称的测试,其中包括Python使用文件名,类名和函数名做为变量的运算符。 上面的例子将运行TestMyClass.test_something但不运行TestMyClass.test_method_simple

4.按节点运行

每一个收集的测试都分配了一个惟一的nodeid,它由模块文件名和后跟说明符组成来自参数化的类名,函数名和参数,由:: characters分隔。

运行.py模块里面的某个函数

pytest test_mod.py::test_func

运行.py模块里面,测试类里面的某个方法

pytest test_mod.py::TestClass::test_method

5.标记表达式

pytest -m slow

将运行用@ pytest.mark.slow装饰器修饰的全部测试,slow是本身命名的标记,能够自定义

import pytest @pytest.mark.finished def test_send_http(): pass  


def test_something_quick(): pass

运行测试时使用-m选项能够加上逻辑

>pytest -m "finished and commit" //匹配finished和commit运行

>pytest -m "finished and not merged" //finished运行,merged不运行

6.从包里面运行

pytest --pyargs pkg.testing

这将导入pkg.testing并使用其文件系统位置来查找和运行测试。

7.在第一个(或N个)失败后中止

pytest -x            # stop after first failure
pytest --maxfail=2    # stop after two failures

8.跳过测试

使用pytest.mark.skip标记须要跳过的用例

@pytest.mark.skip(reason="not finished") def test_send_http(): pass

也支持使用 pytest.mark.skipif 为测试函数指定被忽略的条件

@pytest.mark.skipif(finishflag==Fasle,reason="not finished") def test_send_http(): pass

9.脚本调用执行

直接使用 pytest.main() 像命令行同样传递参数 pytest.main(["-x", "mytestdir"])

 

用例编写

断言

pytest直接使用python assert语法来写

def f(): return 3

def test_function(): assert f() == 4

断言中添加消息

assert a % 2 == 0, "value was odd, should be even"

预设与清理

与unittest中的setup和teardown相似,pytest也有这样的环境清理方法,主要有

  • 模块级(setup_module/teardown_module)开始于模块始末,全局的

  • 函数级(setup_function/teardown_function)只对函数用例生效(不在类中)

  • 类级(setup_class/teardown_class)只在类中先后运行一次(在类中)

  • 方法级(setup_method/teardown_method)开始于方法始末(在类中)

  • 类里面的(setup/teardown)运行在调用方法的先后

import pytest class TestClass: def setup_class(self): print("setup_class:类中全部用例执行以前") def teardown_class(self): print("teardown_class:类中全部用例执行以前") def setup_method(self): print("setup_method: 每一个用例开始前执行") def teardown_method(self): print("teardown_method: 每一个用例结束后执行") def setup(self): print("setup: 每一个用例开始前执行") def teardown(self): print("teardown: 每一个用例结束后执行") def test_one(self): print("执行第一个用例") def test_two(self): print("执行第二个用例") def setup_module(): print("setup_module:整个.py模块只执行一次") def teardown_module(): print("teardown_module:整个.py模块只执行一次") def setup_function(): print("setup_function:每一个方法用例开始前都会执行") def teardown_function(): print("teardown_function:每一个方法用例结束前都会执行") def test_three(): print("执行第三个用例")

使用pytest -s test_sample.py运行,-s参数是为了显示用例的打印信息,下面是输出,能够看出几个方法之间的优先级

test_sample.py setup_module:整个.py模块只执行一次 setup_class:类中全部用例执行以前 setup_method: 每一个用例开始前执行 setup: 每一个用例开始前执行 执行第一个用例 .teardown: 每一个用例结束后执行 teardown_method: 每一个用例结束后执行 setup_method: 每一个用例开始前执行 setup: 每一个用例开始前执行 执行第二个用例 .teardown: 每一个用例结束后执行 teardown_method: 每一个用例结束后执行 teardown_class:类中全部用例执行以前 setup_function:每一个方法用例开始前都会执行 执行第三个用例 .teardown_function:每一个方法用例结束前都会执行 teardown_module:整个.py模块只执行一次

注意:setup_method和teardown_method的功能和setup/teardown功能是同样的,通常两者用其中一个便可;函数里面用到的setup_function/teardown_function与类里面的setup_class/teardown_class互不干涉

参数化

使用pytest.mark.parametrize(argnames, argvalues)能够实现函数的参数化

@pytest.mark.parametrize('text',['test1','test2','test3']) def test_one(text): print(text)

argnames就是形参名称,argvalues就是待测的一组数据

 

固件fixture

基本使用

固件Fixture是一些函数,pytest 会在执行测试函数以前(或以后)加载运行它们。主要是为一些单独测试用例须要预先设置与清理的状况下使用的。

不一样于上面的setup和teardown的就是,能够自定义函数,能够指定用例运行,使用方法以下

@pytest.fixture() def text(): print("开始执行")          #使用pytest.fixture()装饰一个函数成为fixture

def test_one(): print("执行第一个用例") def test_two(text):          #用例传入fixture函数名,以此来确认执行
    print("执行第二个用例")

使用yield能够实现固件的拆分运行,yield前在用例前执行,yield后再用例后执行

@pytest.fixture() def text(): print("开始执行") yield                 #yield 关键词将固件分为两部分,yield 以前的代码属于预处理,会在测试前执行;yield 以后的代码属于后处理,将在测试完成后执行
    print("执行完毕") def test_one(): print("执行第一个用例") def test_two(text): print("执行第二个用例")

统一管理

固件能够直接定义在各测试脚本中,就像上面的例子。更多时候,咱们但愿一个固件能够在更大程度上复用,这就须要对固件进行集中管理。Pytest 使用文件 conftest.py 集中管理固件。

不用显式调用 conftest.py,pytest 会自动调用,能够把 conftest 当作插件来理解

./conftest.py @pytest.fixture() def text(): print("开始执行") yield
    print("执行完毕") ./test_sample.py def test_one(): print("执行第一个用例") def test_two(text): print("执行第二个用例")

做用域

fixture能够经过 scope 参数声明做用域,好比

  • function: 函数级,每一个测试函数都会执行一次固件;
  • class: 类级别,每一个测试类执行一次,全部方法均可以使用;
  • module: 模块级,每一个模块执行一次,模块内函数和方法均可使用;
  • session: 会话级,一次测试只执行一次,全部被找到的函数和方法均可用。
./conftest.py @pytest.fixture(scope="module") def text(): print("开始执行") yield
    print("执行完毕") ./test_sample.py def test_one(text): print("执行第一个用例") def test_two(text): print("执行第二个用例")

执行状况

test_sample.py 开始执行 执行第一个用例 .执行第二个用例 .执行完毕

若是对于类使用做用域,须要使用 pytest.mark.usefixtures(对函数和方法也适用)

./conftest.py @pytest.fixture(scope="class") def text(): print("开始执行") yield
    print("执行完毕") ./test_sample.py @pytest.mark.usefixtures('text') class TestClass: def test_one(self): print("执行第一个用例") def test_two(self): print("执行第二个用例")

自动运行

将fixture的autouse参数设置为True时,能够不用传入函数,自动运行

./conftest.py @pytest.fixture(scope="module",autouse=True) def text(): print("开始执行") yield
    print("执行完毕") ./test_sample.py def test_one(): print("执行第一个用例") def test_two(): print("执行第二个用例")

参数化

使用fixture的params参数能够实现参数化

./conftest.py @pytest.fixture(scope="module",params=['test1','test2']) def text(request): print("开始执行") yield request.param print("执行完毕") ./test_sample.py def test_one(text): print("执行第一个用例") print(text) def test_two(text): print("执行第二个用例")

固件参数化须要使用 pytest 内置的固件 request,并经过 request.param 获取参数。

结果以下

test_sample.py 开始执行 执行第一个用例 test1 .执行第二个用例 .执行完毕 开始执行 执行第一个用例 test2 .执行第二个用例 .执行完毕

 

生成报告

HTML报告

安装pytest-html

pip install pytest-html

使用方法是,直接在命令行pytest命令后面加--html=<文件名字或者路径>.html参数就能够了

pytest --html=report.html

结果以下

上面生成的报告包括html和一个assets文件(里面是报告CSS样式),若是要合成一个文件能够添加下面的参数

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

 

XML报告

使用命令能够生成XML格式报告

pytest --junitxml=report.xml

 

allure报告

1.首先安装java环境

下载JDK  http://www.oracle.com/technetwork/java/javase/downloads/index.html 

安装对应系统的包,如windowsx64是xxx-windows-x64.exe

一路下一步安装就能够了

而后在环境变量中添加下面变量

变量名:JAVA_HOME 变量值:C:\Program Files (x86)\Java\jdk1.8.0_91        // 要根据本身的实际路径配置 变量名:CLASSPATH 变量值:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;      //记得前面有个"." 变量名:Path 变量值:%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;

而后再cmd中运行 java -versionjavajavac 几个命令没有报错便可

 

2.安装allure

下载allure 会跳转到github,找到link下载

解压包后进入bin文件,复制路径并添加到环境变量Path中

 cmd运行allure命令,没有报错即正确

 

3.pytest使用

安装插件

pip install allure-pytest 若是timeout就加 --index-url https://pypi.douban.com/simple  用豆瓣源

进入测试py文件目录,运行

pytest --alluredir ./report

运行完成后生成report文件,-alluredir后面跟的是文件路径,能够自定义

使用allure查看报告,直接启动allure server后面加报告路径就行

allure serve report(报告文件夹名)

等一会就生成报告

 

 

 

 

 

参考:

https://learning-pytest.readthedocs.io/zh/latest/index.html

https://docs.pytest.org/en/latest/contents.html

相关文章
相关标签/搜索