import pytest,os,yaml
import allure
class TestClass:
@pytest.mark.run(order=3) #借助于装饰器@pytest.mark.run()控制测试运行的顺序,前提需pip install pytest-ordering
def test_one(self):
x = "this"
assert 'h' in x #h在x中
@pytest.mark.run(order=1)
def test_two(self):
# x = "hello"
# assert hasattr(x, 'check')
class Yt():
def g(self):
print("666")
x=Yt()
assert hasattr(x,'g') #检查对象是否符合预期的结构或拥有特定的属性
@pytest.mark.run(order=2)
def test_three(self):
a = "hello"
b = "hello world"
assert a in b #a是否在b中
# yaml.safe_load(open('./te1.yaml'))
@pytest.mark.parametrize("a",[2]) # 用例的参数化装饰/a,b为要参数化的变量,后面为值。list[tuple],以列表形式传入元组,每个元组都是一条测试数据
@pytest.mark.run(order=5)
def test_param(self,a):
print(" {}".format(a))
assert a == 2
@pytest.mark.flaky(reruns=1, reruns_delay=3)#失败用例重试。重新运行1次,每间隔3秒钟
@pytest.mark.run(order=4)
def test_tty(self):
a = "hello"
b = "hellopi world"
assert a in b
# pytest --reruns 1 --reruns-delay 3 命令行使用,失败用例重试。重新运行1次,每间隔3秒钟//pip insatll pytest- rerunfailures后生效;在装饰器中示例@pytest.mark.flaky(reruns=3, reruns_delay=5)
#
if __name__ == "__main__":
#1
pytest.main('-q run.py')#运行查看整体测试结果//pytest.main方法自动读取当前目录下的所有test开头的.py文件,运行test方法或者类
# pytest.main(['./run.py::test_tw'])#单独运行某条用例
# pytest.main(['./run.py::test_param'])
# pytest.main(["test_debug.py","-v","--html=./report0414.html", # 生成测试报告"--self-contained-html", # 把css样式合并到html里
# "--color=yes" # pytest 写入输出颜色
# ])
# """
# -v 用于显示每个测试函数的执行结果
# -q 只显示整体测试结果
# -s 用于显示测试函数中print()函数输出
# -x, --exitfirst, exit instantly on first error or failed test
# -m 只运行带有装饰器配置的测试用例
# -h 帮助
# """
#2
# os.system('py.test -q run.py --html=report.html')#pip install -U pytest-html生成html报告,生成报告命令cmd中使用
#3
# os.system('py.test --cov-report=html --cov=./run.py')#pip install pytest-cov生成更详细的报告,生成报告命令cmd中使用
# """
# pytest_cov参数:
# --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,则认为失败
# """
#4
# coverage run - m pytest. 用于在运行pytest测试时使用Coverage进行代码覆盖率分析。
yaml示例:
by: id locator: name action: click
import yaml,pytest
ii=yaml.safe_load(open('./te1.yaml'))
print(ii)
@pytest.mark.parametrize("a",[ii])
def test_param(a):
# e=a['by']
# print("{}".format(e))
print(a.get("by"))