3.pytest函数级测试用例和类级测试用例

pytest中可以存在函数级测试用例和类级测试用例。且类级别测试用例不需要继承,只需遵守pytest命名规则。

1.函数级测试用例

def test_add_1():
    assert add(1,2)==3

def test_add_2():
    assert add(2,3)==4

def test_add_a():
    assert add(2,3)==5

执行顺序为:test_add_1  test_add_2  test_add_a

2.类级别测试用例,类级别测试用例中不可以存在__init__方法

class Test_class():

    def test4(self):
        assert add(3,5)==8

    def test5(self):
        assert add(6,7)==13

运行顺序为:test4  test5

如果一个文件中同时存在函数级别和类级别测试用例则会先执行函数级别

原文地址:https://www.cnblogs.com/XhyTechnologyShare/p/12251795.html