栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pytest测试框架应用

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pytest测试框架应用

函数的执行规则

import pytest
def test_login():
    print('执行A')
def testlogin():
    print('执行B')
def logintest():
    print('不执行A')
def login_test():
    print('不执行B')
def logintesting():
    print('不执行C')
pytest.main(['-s', '-v', 'test_function_rule.py'])

方法的执行规则

import pytest
# 定义TestOrdering类
class TestOrdering():
    def test_login(self):
        print("正在登录")
    def test_add(self):
        print("正在增加")
    def test_del(self):
        print("正在删除")
if __name__ == '__main__':
    pytest.main(['-s', '-v', 'test_method_rule.py'])

单个参数的字符串参数化

import pytest
# 一个参数的参数化。依次取列表中的字符串,然后打印
@pytest.mark.parametrize("username", ["daA", "daB", "daC"])
def test_login(username):
    # f格式化字符串
    print(f"登录成功:登录用户为{username}")
if __name__ == '__main__':
    pytest.main(['-v', '-s', 'test_Parametrize_String.py'])

单个参数的元组参数化

import pytest
# 一个参数的参数化,userid依次取列表中的两个元组对象,然后打印
@pytest.mark.parametrize("userid", [(1, 2), (3, 4)])
def test_login(userid):
    print(f"登录成功:登录id为{userid}")
if __name__ == '__main__':
    pytest.main(['-v', '-s', 'test_Parametrize_tuple.py'])

单个参数的字典参数化

import pytest
@pytest.mark.parametrize("register", [{'name': 'daA'}, {'password': 666666}])
def test_register(register):
    print(f"注册成功:注册信息为{register}")
if __name__ == '__main__':
    pytest.main(['-v', '-s', 'test_Parametrize_dictionaries.py'])

多个参数的字符串参数化

import pytest
# 多个参数的参数化
@pytest.mark.parametrize("username", ["daA", "daB", 'daC'])
@pytest.mark.parametrize("password", ["test12", "test34", "test56"])
def test_register(username, password):
    print(f"登录成功:用户名为{username},密码为{password}")
if __name__ == '__main__':
    pytest.main(['-v', '-s', 'test_Parametrize_string_duo.py'])

多个参数的字符串参数化

import pytest
# 多个参数的参数化,同步取值
@pytest.mark.parametrize("username,password", [("daA", "A111"), ("daB", "B222")])
def test_register(username, password):
    print(f"登录成功:用户名为{username},密码为{password}")
if __name__ == '__main__':
    pytest.main(['-v', '-s', 'test_Parametrize_tuple_duo.py'])

assert原生断言

import pytest
x = 1
def test_int_001():
    assert x
if __name__ == '__main__':
    pytest.main(['-s', '-v', 'test_01.py'])
import pytest
x = 0
def test_int_002():
    assert not x
if __name__ == '__main__':
    pytest.main(['-s', '-v', 'test_02.py'])
import pytest
x = 'th'
y = 'python'
def test_str_001():
    assert x in y
def test_str_002():
    assert y in x
if __name__ == '__main__':
    pytest.main(['-s', '-v', 'test_02.py'])
import pytest
x = 'python'
y = 'python'
z = 'java'
def test_str_001():
    assert x == y
def test_str_002():
    assert x == z
if __name__ == '__main__':
    pytest.main(['-s', '-v', 'test_04.py'])
import pytest
x = 'python'
y = 'python'
z = 'java'
def test_str_001():
    assert x != y
def test_str_002():
    assert x != z
if __name__ == '__main__':
    pytest.main(['-s', '-v', 'test_05.py'])
setp和teardown的五种类型

模块级别

import pytest
def setup_module():
    print('用例前置')
def teardown_module():
    print('用例后置')
def test01():
    print('用例1')
def test02():
    print('用例2')
if __name__ == '__main__':
    pytest.main(['-s', '-v', 'test_setup_module.py'])

函数级别

import pytest
def setup_function():
    print('用例前置')
def teardown_function():
    print('用例后置')
def test01():
    print('用例1')
def test02():
    print('用例2')
if __name__ == '__main__':
    pytest.main(['-s', '-v', 'test_setup_function.py'])

类级别

import pytest
class Test():
    def setup_class(self):
        print('类前置')
    def teardown_class(self):
        print('类后置')
    def test01(self):
        print('用例1')
    def test02(self):
        print('用例2')
if __name__ == '__main__':
    pytest.main(['-s', '-v', 'test_setup_class.py'])

类方法级别

import pytest
class Test():
    def setup_method(self):
        print('方法前置')
    def teardown_method(self):
        print('方法后置')
    def test01(self):
        print('用例1')
    def test02(self):
        print('用例2')
if __name__ == '__main__':
    pytest.main(['-s', '-v', 'test_setup_method.py'])

类方法细化级别

import pytest
class Test():
    def setup_method(self):
        print('类方法前置')
    def teardown_method(self):
        print('类方法后置')
    def setup(self):
        print('类方法细化前置')
    def teardown(self):
        print('类方法细化后置')
    def test01(self):
        print('用例1')
    def test02(self):
        print('用例2')
if __name__ == '__main__':
    pytest.main(['-v', '-s', 'test_setup.py'])
配置文件设置
[pytest]
addopts = -s -v --html=../report/report.html
testpaths = testcase
python_files = test_*.py
python_classes = Test*
python_functions = test*
# testpaths 定义测试用例文件夹名称,只有testcase的文件夹下
# python_files 定义测试用例所在的文件,只有文件名是test的
# python_classes 定义测试用例文件中的类名称,只有Test的
# python_functions 定义函数或方法名称,只有test的
生成测试报告
import pytest
def test_login():
    print('A')
def testlogin():
    print('B')
if __name__ == '__main__':
    pytest.main(['-s', '-v', 'test_api.py'])

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/926251.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号