一般来说,自动化脚本是由若干个测试套组成的,而每个测试套又由测试套setup,若干个脚本,测试套teardown组成的。每个脚本又是由测试脚本级setup,测试脚本正文、测试脚本级teardown组成,具体结构如下所示。
测试套1
|--------测试套层级的setup
|--------测试脚本1
|--------测试脚本层级的setup
|--------测试脚本1正文
|--------测试脚本层级的teardown
|--------测试脚本2
|--------测试脚本层级的setup
|--------测试脚本2正文
|--------测试脚本层级的teardown
|--------测试套层级的teardown
测试套2
|--------测试套层级的setup
|--------测试脚本3
|--------测试脚本层级的setup
|--------测试脚本3正文
|--------测试脚本层级的teardown
|--------测试脚本4
|--------测试脚本层级的setup
|--------测试脚本4正文
|--------测试脚本层级的teardown
|--------测试套层级的teardown
在pytest自动化测试框架中,测试套可以是一个类,也可以是一个文件,测试脚本可以是类中的一个测试函数,也可以是文件中的一个测试函数,下面就将pytest中的测试函数、测试类、以及各个层级的setup和teardown详细展开讲解。
在pytest中如果使用类组织测试脚本,则一个类相当于是一个测试套,测试套就有测试套级别的setup和teardown,如下代码,类级别的setup和teardown的名字分别为setup_class和teardown_class,因为类中的函数准确的说应该叫方法,因此类中的测试脚本级的setup和teardown的的名字分别为setup_method和teardown_method。如下代码中有两个测试函数test_01和test_02,因此从理论上分析如下代码的执行顺序应该是首先执行测试套级的setup,即setup_class,然后执行第一个脚本,即setup_method,test_01,teardown_method,接下来执行第二个脚本,即setup_method,test_02,teardown_method,最后执行测试套级的teardown,即teardown_class
class TestDemo:
def setup_class(self):
print("in setup_class")
def teardown_class(self):
print("in testdown_class")
def setup_method(self):
print("in setup_method")
def teardown_method(self):
print("in teardown_method")
def test_01(self):
print("in test_01")
assert 1==1
def test_02(self):
print("in test_02")
使用pytest -s命令执行,结果如下,通过打印的信息可以看出执行顺序与前面理论分析的顺序是一致的。
$ pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.13, pytest-7.1.2, pluggy-1.0.0
rootdir: D:\redrose2100-book\ebooks\Pytest企业级应用实战\src
collected 2 items
test_demo.py in setup_class
in setup_method
in test_01
.in teardown_method
in setup_method
in test_02
.in teardown_method
in testdown_class
========================================================================== 2 passed in 2.14s ===========================================================================
测试文件又叫测试模块,即module,在pytest框架中,一个测试文件同样也可以作为一个测试套来组织管理测试脚本,如下代码中即为一个测试文件组织管理了两个测试脚本test_03和test_04,既然是测试套就同样有测试套层级setup和teardown的概念,在这里测试套层级的setup和teardown的名字为setup_module和teardown_module,在python文件中的函数即叫做函数,所以测试模块中测试脚本层级的setup和teardown的名字为setup_function和teardown_function,如下,那么这里从理论上分析执行的顺序应该是首先执行测试套的setup,即setup_module,然后执行第一个测试脚本,即setup_function,test_03,teardown_function,然后执行第二个脚本,即setup_function,test_04,teardown_function,最后执行测试套级别的teardown,即teardown_module。
def setup_module():
print("in setup_module")
def teardown_module():
print("in teardown_module")
def setup_function():
print("in setup_function")
def teardown_function():
print("in teardown_function")
def test_03():
print("in test_03")
assert 1==1
def test_04():
print("in test_04")
assert 1==1
使用pytest -s命令执行,结果如下,通过打印的顺序可以看出这里的执行顺序与前面的理论分析的顺序是完全一致的。
$ pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.13, pytest-7.1.2, pluggy-1.0.0
rootdir: D:\redrose2100-book\ebooks\Pytest企业级应用实战\src
collected 2 items
test_demo.py in setup_module
in setup_function
in test_03
.in teardown_function
in setup_function
in test_04
.in teardown_function
in teardown_module
========================================================================== 2 passed in 2.07s ===========================================================================
当测试文件中测试函数和类中的测试方法混合时,如下测试脚本,看上去执行顺序非常复杂,其实这里面可以把测试类TestDemo看做与test_03和test_04同一级别的来分析,这样就很容分析出执行的顺序了,即首先执行测试模块级的setup,即setup_module,然后执行第一个脚本,这里第一个可以理解为TestDemo这个测试了,此时就可以按照测试类的执行原理将TestDemo中的代码执行完成,然后执行第二个脚本,即setup_function,test_03,teardown_function,接下来执行第三个脚本,即setup_function,test_04,teardown_function,最后执行测试模块级的teardown,即teardown_module。
class TestDemo:
def setup_class(self):
print("in setup_class")
def teardown_class(self):
print("in testdown_class")
def setup_method(self):
print("in setup_method")
def teardown_method(self):
print("in teardown_method")
def test_01(self):
print("in test_01")
assert 1==1
def test_02(self):
print("in test_02")
def setup_module():
print("in setup_module")
def teardown_module():
print("in teardown_module")
def setup_function():
print("in setup_function")
def teardown_function():
print("in teardown_function")
def test_03():
print("in test_03")
assert 1==1
def test_04():
print("in test_04")
assert 1==1
使用pytest -s命令执行,结果如下,通过打印内容可以看出执行顺序与前面的理论分析的顺序是一致的。
$ pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.13, pytest-7.1.2, pluggy-1.0.0
rootdir: D:\redrose2100-book\ebooks\Pytest企业级应用实战\src
collected 4 items
test_demo.py in setup_module
in setup_class
in setup_method
in test_01
.in teardown_method
in setup_method
in test_02
.in teardown_method
in testdown_class
in setup_function
in test_03
.in teardown_function
in setup_function
in test_04
.in teardown_function
in teardown_module
========================================================================== 4 passed in 0.02s ===========================================================================
通过前面对各个层级的setup和teardown的执行顺序的分析,可以看出,测试套层级的setup主要用于测试套中所有的测试脚本公共的而且只需要配置一次的配置,而测试套层级的teardown则用于当测试套内的所有用例执行完成后,恢复环境或者清理配置的作用。比如当一个测试套中的所有用例都需要某个用户登录之后才能才做,此时就可以将登录操作在测试套级别的setup中实现,而退出登录操作则可在测试套级别的teardown中实现。而对于测试套中的每个用例,比如当用户登录后,在每个用例中都希望首先将页面切换到首页,然后再进行相关的操作,此时就可以将切换到首页的动作在测试脚本级的setup中实现,即只需要实现一次,当前测试套中的每个用例执行之前都会首先执行脚本级setup操作,即切换到首页,这样一来就减少了测试脚本的代码重复率同时也提高了脚本的运行效率。
当然在自动化实践中,需要提前给测试人员传达这种思想,目的是让测试在设计用例的时候就要把用例按照测试套的维度去划分。