固定输入校验
# -*- coding: utf-8 -*- # @Time : 2021/10/10 # @Author : 大海 # @File : test_13.py import pytest """ 参数说明: 第1个参数是字符串,多个参数中间用逗号隔开 第2个参数是list,多组数组用元组类型 """ @pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6 * 9", 54), ]) def test_eval(test_input, expected): assert eval(test_input) == expected if __name__ == "__main__": pytest.main(["-s", "test_13.py"])
参数组合
# -*- coding: utf-8 -*- # @Time : 2021/10/10 # @Author : 大海 # @File : test_14.py import pytest # 遍历x与y组合 @pytest.mark.parametrize("x", [0, 1]) @pytest.mark.parametrize("y", [2, 3]) def test_foo(x, y): print("测试数据组合:x->%s, y->%s" % (x, y)) if __name__ == "__main__": pytest.main(["-s", "test_14.py"])
mark.xfail标记失败用例
# -*- coding: utf-8 -*- # @Time : 2021/10/10 # @Author : 大海 # @File : test_15.py import pytest """ 标记为失败的用例,预期结果是失败,实际运行也是失败,显示xfailed 使用场景: 功能已经未开发完或有问题,可以使用此标记为预期失败 """ @pytest.mark.parametrize("test_input,expected", [ ("3+5", 8), ("2+4", 6), pytest.param("6 * 9", 42, marks=pytest.mark.xfail), ]) def test_eval(test_input, expected): print("-------开始用例------") assert eval(test_input) == expected if __name__ == "__main__": pytest.main(["-s", "test_15.py"])
fixtrue和parametrize搭配使用
# -*- coding: utf-8 -*- # @Time : 2021/10/10 # @Author : 大海 # @File : test_16.py import pytest # 测试账号数据 test_data = ["user1", "user2"] @pytest.fixture(params=test_data) def start(request): user = request.param print('启动app') print("登录账户:%s" % user) return user # indirect=True 参数是为了把start当作一个函数去执行,而不是一个参数 @pytest.mark.parametrize("start", test_data, indirect=True) def test_login(start): """登录用例""" a = start print("start中的返回值:%s" % a) assert a != "" if __name__ == "__main__": pytest.main(["-s", "test_16.py"])