pytest 基础知识点整理

一、pytest使用步骤:

1、导入pytest

2、编写测试用例

无需在测试类下编写测试用例,可以直接编写测试函数

测试函数名中必须包含test_,_test

3、pytest框架下执行测试用例

py文件内执行测试用例

pytest.main(“-s test_case_01.py”)

运行整个当前./test_case目录

pytest.main(['-q', './test_case',

             '--html=./report/yqyx_report_{}.html'.format(ts)])

-s输出的详情测试结果

4、.表示执行通过;F表示用例执行失败

二、pytestsetupteardown函数

pytestsetupteardown函数

模块级别:对整个.py文件作用setup_moduleteardown_module

函数级别:对测试用例作用(不在测试类中)

类级别:setup_classteardown_class

方法级别:setup_methodteardown_method

总结:

pytest测试方法:

测试类的类名Test开关,

测试类中不需要__init__方法

测试类中的测试方法的编写和测试函数的编写规则一致

 

三、pytest安装

1、安装Pytest包:pip install pytest  

2、安装python-html包:pip install pytest-html

3、安装allure包:生成allure报告:pip install allure-pytest

 

四、pytests配置文件

1、

1.1在主函数中执行 pytest.main('-s test_case_03.py')

1.2命令行执行方式:在测试用例所在目录下进入命令行,输入pytest -s test_case_02.py

1.3在项目根目录下进入命令行:pytest[参数]执行文件

1.4 pycharm中,使用Terminal选项代替命令行操作

五、pytests常用插件

1、测试报告

安装库:pip install pytest-html

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

使用方法:在配置文件中添加参数 addopts = -s --html=report/report.html

 

另一种格式报告:

A、下载allure生成工具 allure-2.7.0.zip

B、解压allure-2.7.0.zip

C、allure-2.7.0.zip工具下的bin目录D:\pro\allure-2.7.0\bin 配置到path环境变量里

(此电脑-属性-高级系统设置-环境变量-系统变量-path

D、

D.1、首先生成allure结果目录:'--alluredir','./report/reportallure/

D.2、进入report报告的目录,运行 allure generate ./reportallure/ -o ./reporthtml/ --clean

 

 

2、控制测试用例执行顺序

安装库:pip install pytest-ordering

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

使用方法:在被执行的用例前添加 @pytest.mark.run(order=x)

执行顺序:0>正数>没有修饰>负数

import pytest

class TestOrdering:
    @pytest.mark.run(order=0.1)
    def test_login(self):
        print("\n order=0.1")
        assert 1

    @pytest.mark.run(order=0.2)
    def test_register(self):
        print('order=0.2')
        assert 1

    @pytest.mark.run(order=1)
    def test_shopping(self):
        print('order=1')
        assert 1

    @pytest.mark.run(order=0)
    def test_car(self):
        print('order=0')
        assert 1

    @pytest.mark.run(order=-1)
    def test_shopping_1(self):
        print('order=-1')
        assert 1

    @pytest.mark.run(order=-0.2)
    def test_shopping_2(self):
        print('order=-0.2')
        assert 1

    @pytest.mark.run(order=-0.1)
    def test_shopping_3(self):
        print('order=-0.1')
        assert 1

    def test_shopping_4(self):
        print('没有修饰')
        assert 1

# #结果如下:
# order=0
#  order=0.1
# .order=0.2
# .order=1
# .没有修饰
# .order=-1
# .order=-0.2
# .order=-0.1

 

3、失败重试

安装库:pip install pytest-rerunfailures

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

使用方法:在配置文件中命令行参数中添加新参数  --reruns=2 失败重跑2次;在失败重跑次数内执行通过了,剩余的重跑,次数将不再执行

4、跳过测试

Skipif(confition,reason) confition跳过条件,reason原因

使用方法:在被执行的用例前添加 @pytest.mark.skipif(条件,跳过原因)

5、标记为失败

Xfail(condition,reson)

condition预期失败的条件,当条件为真的时候预期失败

reson原因

遇到的情况:

预期失败-结果失败

预期失败-结果成功

预期成功-结果失败

预期成功-结果成功

在配置文件中添加一个参数:xfail_strict=true

6、参数化

语法 parametrize(argnames,argvalues)  argnames参数名,argvalues参数值

使用方式:在测试函数前使用  @pytest.mark.parametriae(argnames,argvalues)

7、Fixture

优势:pytestfixtrue命名不局限于setupteardown命名方式

所有的fixtrue都可以写在一个conftest.py的文件中,供的有测试用例使用

fixture创建和使用方法:

l fixtrue创建  

@pytest.fixture()

def login():

print(“执行登陆”)

l fixture使用

def test_shopping(login)

print(“执行登陆”)

 

8、Jenkins

cd /var/lib/jenkins/workspace/api_saas/test_case

python -m pytest --html-result.html --junit-xml=result.xml --alluredir ${WORKSP}/allure-results

 

--alluredir ${WORKSP}/allure-results:生成报告allure${WORKSP}变量组成路径

python -m pytest: python运行pytest框架

--html-result.html: 生成html报告

--junit-xml=result.xml:生成xml报告

 

 

发送对应邮件:配置收件人、配置收件的格式

 

posted @ 2020-09-11 14:01  LCX测试小姐姐  阅读(403)  评论(0编辑  收藏  举报