用例搜索及默认执行规则

前言:

在一个测试框架当中,测试用例模块,测试类,或者具体测试用例是有其规定的命名规则的,测试框架进行用例搜索也是有规则的

一、用例命名规则:

1. 测试包:test开头,必须含有__init__.py。

2. 测试模块: test_*.py或 *_test.py。

3. 测试类:Test开头,但不含有__init__()方法。

4. 测试用例:test_开头。

二、pytest框架搜索用例规则:

pytest会在当前目录及其子目录下搜索测试用例,识别用例的规则需符合用例命名规则。

三、用例默认执行顺序:

默认情况下,pytest会按照从上至下的顺序执行。

示例:

def test_c():
    print("this is test_c")


class TestA:
    def test_01(self):
        print("this is test_01")

    def test_03(self):
        print("this is test_03")

    def test_02(self):
        print("this is test_02")

    def test_b(self):
        print("this is test_b")

执行结果:

collecting ... collected 5 items

test_a.py::test_c PASSED                                                 [ 20%]this is test_c

test_a.py::TestA::test_01 PASSED                                         [ 40%]this is test_01

test_a.py::TestA::test_03 PASSED                                         [ 60%]this is test_03

test_a.py::TestA::test_02 PASSED                                         [ 80%]this is test_02

test_a.py::TestA::test_b PASSED                                          [100%]this is test_b

 

四、思考:

1.pytest中需自定义用例执行顺序,该怎么办?

2.一个package中,默认的用例执行顺序也是从上到下吗?

posted @ 2022-02-21 13:31  Target_L  阅读(72)  评论(1)    收藏  举报