pytest命名规则
- 测试文件以test_开头(以_test结尾也可以)
- 测试类以Test开头(驼峰命名法),并且不能带有 init 方法
- 测试函数以test_开头
- 断言使用asser
fixture函数\修饰器
@pytest.fixture(scope="function", params=None, autouse=False, ids=None, name=None)
def test():
print("fixture初始化的参数列表")
scope参数有四种,分别是’function’,’module’,’class’,’session’,默认为function。
- function:每个test都运行,默认是function的scope
- class:每个class的所有test只运行一次
- module:每个module的所有test只运行一次
- session:每个session只运行一次
autouse:默认:False,需要用例手动调用该fixture;如果是True,所有作用域内的测试用例都会自动调用该fixture
name:默认:装饰器的名称,同一模块的fixture相互调用建议写个不同的name
- 较高 scope 范围的fixture(session)在较低 scope 范围的fixture( function 、 class )之前实例化【session > package > module > class > function】
- 具有相同作用域的fixture遵循测试函数中声明的顺序,并遵循fixture之间的依赖关系【在fixture_A里面依赖的fixture_B优先实例化,然后到fixture_A实例化】
- 自动使用(autouse=True)的fixture将在显式使用(传参或装饰器)的fixture之前实例化
import pytest
order = []
@pytest.fixture(scope="session")
def s1():
order.append("s1")
@pytest.fixture(scope="module")
def m1():
order.append("m1")
@pytest.fixture
def f1(f3, a1):
order.append("f1")
assert f3 == 123
@pytest.fixture
def f3():
order.append("f3")
a = 123
yield a
@pytest.fixture
def a1():
order.append("a1")
@pytest.fixture
def f2():
order.append("f2")
def test_order(f1, m1, f2, s1):
assert order == ["s1", "m1", "f3", "a1", "f1", "f2"]
本段代码断言成功
- 可以通过在fixture函数中通过yield实现setup和teardown功能
- 如果yield前面的代码,即setup部分已经抛出异常了,则不会执行yield后面的teardown内容
- 如果测试用例抛出异常,yield后面的teardown内容还是会正常执行
mark.parametrize装饰器
可以实现测试用例参数化
@pytest.mark.parametrize('参数名',list)
- 第一个参数是一个字符串,多个参数之间用逗号分隔
[En]
the first parameter is a string, separated by commas among multiple parameters*
- 第二个参数是list,多组数据用元祖类型;传三个或更多参数也是这样传。list的每个元素都是一个元组,元组里的每个元素和按参数顺序一一对应
- 传一个参数 @pytest.mark.parametrize(‘参数名’,list) 进行参数化
- 传两个参数@pytest.mark.parametrize(‘参数名1,参数名2’,[(参数1_data[0], 参数2_data[0]),(参数1_data[1], 参数2_data[1])])
- 参数化,当修饰为方法时,该方法执行两次
[En]
parameterized, when decorated to a method, the method is executed twice*
*
- 第1次:参数名1 对应值 参数1_data[0],参数名2 对应值 参数2_data[0]
* - 第2次:参数名1 对应值 参数1_data[1],参数名2 对应值 参数2_data[1],这样就可以用到我们测试用例执行中,根据用例的多少,调用多次,断言多次,不需要用循环去写了。
–
[En]
–
版权声明:本文为CSDN博主「王大力测试进阶之路」的原创文章,遵循CC 4.0
BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36502272/article/details/102872332
例子:
fil = yaml.safe_load(open('./testcase.yaml', encoding='utf-8'))
print(fil['case_add']['test_data'])
print(fil['case_add']['ids'])
@pytest.mark.parametrize('a,b,c', fil['case_add']['test_data'], ids=fil['case_add']['ids'])
def test_code1(self, a, b, c):
print(a, b, c)
assert Code.code1(self, a, b) == c
其中yaml文件的组织形式为
case_add:
test_data:
-
- 1
- 1
- 2
-
- 2
- 2
- 4
ids:
- 11
- 22
返回结果为:
[[1, 1, 2], [2, 2, 4]]
[11, 22]
setup和teardown操作
在pytest中setup和teardown操作与unittest中相似。
命令行形式
pytest
pytest test_mod.py
pytest somepath
pytest -k stringexpr
pytest test_mod.py::test_func
通过 pytest.mark装饰器对测试的test进行分类执行
通过通过@pytest.mark控制需要执行哪些feature的test,例如在执行test前增加修饰@pytest.mark.website
- 通过-m “website” 执行有website标记的test方法
pytest -v -m "website" pytest1.py
- 通过 -m “not website” 执行没有website标记的test方法
pytest -v -m "not website" pytest1.py
- -v 显示每个测试函数的执行结果
- -q 只显示整体测试结果
- -s 显示测试函数中print()函数输出
- -x 在遇到的第一个错误时退出测试
- -h 帮助
使用pytest.raises捕获程序抛出的异常
def test_div_error(self,a,b,expect):
with pytest.raises(ZeroDivisionError):
self.calc.div(a,b)
上述用例可通过而不报错
测试报告
pytest可以方便的生成测试报告,即可以生成HTML的测试报告,也可以生成XML格式的测试报告用来与持续集成工具集成。
生成HTML格式报告
py.test --resultlog=path
生成XML格式的报告:
py.test --junitxml=path
测试报告
计算pytest覆盖率,支持输出多种格式的测试报告
pip install pytest-cov
pytest –cov-report=html –cov=./ test_code_target_dir
- –cov=[path], measure coverage for filesystem path (multi-allowed), 指定被测试对象,用于计算测试覆盖率
- –cov-report=type, type of report to generate: term, term-missing, annotate, html, xml (multi-allowed), 测试报告的类型
- –cov-config=path, config file for coverage, default: .coveragerc, coverage配置文件
- –no-cov-on-fail, do not report coverage if test run fails, default: False,如果测试失败,不生成测试报告
- –cov-fail-under=MIN, Fail if the total coverage is less than MIN. 如果测试覆盖率低于MIN,则认为失败
pytest1.py 18 0 100%
Original: https://blog.csdn.net/qq_36569690/article/details/115257903
Author: Nicooo-929
Title: Python3 零基础自学笔记_pytest框架
相关阅读
Title: Python numpy中random函数的使用
np.random:随机数的生成
- *np.random.random()
import numpy as np
c = np.random.random() #生成一个(0,1)之间的随机浮点数
print('c的值:',c)

- *np.random.random(size)
import numpy as np
c = np.random.random(5) #生成size个(0,1)之间的随机浮点数
print('c的值:',c)

- *np.random.random([m,n])或np.random.random((m,n))
import numpy as np
c = np.random.random([2,6]) #生成m行n列的(0,1)之间的随机浮点数
print('c的值:')
print(c)

- np.random.rand(m,n)
- 与np.random.random((m,n))作用一样,但是参数形式不同。
import numpy as np
c = np.random.rand(2,6) #生成m行n列的(0,1)之间的随机浮点数
print('c的值:')
print(c)

- *np.random.randint(a,b,size)
import numpy as np
c = np.random.randint(0,2,2) #生成size个[a,b)之间的随机整数
print('c的值:')
print(c)

- *np.random.uniform(a,b,size)
import numpy as np
c = np.random.uniform(0,1,2) #生成size个[a,b)之间的随机浮点数
print('c的值:')
print(c)

- *np.random.normal():均值为0,标准差为1【无参默认值】
import numpy as np
c = np.random.normal() #生成一个随机浮点数,随机概率与均值为0,标准差为1的正态分布一致【无参默认值】
print('c的值:')
print(c)

- *np.random.normal(a,b)
import numpy as np
c = np.random.normal(0,1) #生成一个随机浮点数,随机概率与均值为a,标准差为b的正态分布一致
print('c的值:')
print(c)

- *np.random.normal(a,b,size)
import numpy as np
c = np.random.normal(0,1,2) #生成size个随机浮点数,随机概率与均值为a,标准差为b的正态分布一致
print('c的值:')
print(c)


上述正态分布公式转载于https://zhidao.baidu.com/question/431881117.html
Original: https://blog.csdn.net/weixin_45251017/article/details/124570719
Author: 一只草莓熊
Title: Python numpy中random函数的使用
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/337085/
转载文章受原作者版权保护。转载请注明原作者出处!