前言:
上一篇讲到fixture通过scope参数控制setup级别,不填的时候默认 scope="function",那么接下来就会讲scope="module"。
1.fixture参数scope=”module”,module作用是整个.py文件都会生效,用例调用时,参数写上函数名称就行。
看以下代码:
#!/usr/bin/env/python # -*-coding:utf-8-*- # authour:xiapmin_pei import pytest @pytest.fixture(scope="module") def open(): print("打开浏览器,并且打开百度首页") def test_s1(open): print("用例1:搜索python-1") def test_s2(open): print("用例2:搜索python-2") def test_s3(open): print("用例3:搜索python-3")
运行结果:从结果看出,虽然test_s1,test_s2,test_s3三个地方都调用了open函数,但是它只会在第一个用例前执行一次
看第二个例子:如果test_s1不调用,test_s2(调用open),test_s3不调用,运行顺序会是怎样的?
#!/usr/bin/env/python # -*-coding:utf-8-*- # authour:xiapmin_pei import pytest @pytest.fixture(scope="module") def open(): print("打开浏览器,并且打开百度首页") def test_s1(): print("用例1:搜索python-1") def test_s2(open): print("用例2:搜索python-2") def test_s3(): print("用例3:搜索python-3")
运行结果:从结果看出,module级别的fixture在当前.py模块里,只会在用例(test_s2)第一次调用前执行一次