pytest-conftest.py作用范围

1.目录结构

调用fixture有两种写法: 1.装饰器@pytest.mark.usefixtures("start") ; 2.直接在用例里面调用fixture装饰的方法当作参数输入def test_demo(self,start);3.设置fixture参数autouse=True

2.用例传fixture参数

1.项目跟目录下面的全局conftest.py

1 import pytest
2 @pytest.fixture()
3 def start():
4     print("\n全局conftest")

2.test_case_demo/conftest.py 和 test_demo.py

conftest.py 代码

1 import pytest
2 @pytest.fixture()
3 def start_1():
4     print("\n局部test_case_demo")
test_demo.py 代码

1 import pytest 2 class Test_demo():
3     def test_demo(self,start,start_1):  #标记代码
4         assert 1==1
5
6 if __name__ == ‘__main__‘:
7     pytest.main(["-s","-v","test_demo.py"])

运行结果:

3.test_case_demo_2/conftest.py 和 test_demo_2.py

conftest.py 代码

1 import pytest
2 @pytest.fixture()
3 def start_2():
4     print("\n局部test_case_demo_2")
test_demo_2.py 代码1 import pytest2 class Test_demo_2():
3     def test_demo_2(self,start,start_1):  #标记代码
4         assert 1==1
5
6 if __name__ == ‘__main__‘:
7     pytest.main(["-s","test_demo_2.py","-v"])

运行结果可以看出,start起到全局作用,test_case_demo目录下的start_1不管它的运行级别是什么,也只能在test_case_demo下面被调用。
test_demo_2用例不能跨模块调用test_case_demo模块下的start_1,所以test_demo_2用例会运行失败

3.装饰器usefixtures

fixture有 function(默认选),class,module,session,四个级别

一.只有一个.py用例文件

1)定义为@pytest.fixture(scope="function")

跟目录下的conftest.py 代码1 import pytest
2 @pytest.fixture(scope="function")
3 def start():
4     print("\n全局conftest")
5
6 @pytest.fixture()
7 def iniv():
8     print("\n全局iniv")
test_demo.py 代码
1 import pytest
 2 @pytest.mark.usefixtures("start")  # 因为他们级别都是function,所以先运行iniv在运行start
 3 @pytest.mark.usefixtures("iniv")
 4 class Test_demo():
 5     def test_demo(self):
 6         assert 1==1
 7     def test_demo_1(self):
 8         assert 1==1
 9
10 if __name__ == ‘__main__‘:
11     pytest.main(["-s","-v","test_demo.py"])

运行结果:start和iniv两个fixture都打印了

2)定义为@pytest.fixture(scope="class")

1 #跟目录下的conftest.py 代码
2 import pytest
3 @pytest.fixture(scope="class")     #这个是装饰类
4 def start():
5     print("\n我是一个装饰class的start")
6
7 @pytest.fixture(scope="function")   #这个是装饰用例
8 def iniv():
9     print("\n我是一个装饰function的iniv")
 1 import pytest
 2 @pytest.mark.usefixtures("start")
 3 @pytest.mark.usefixtures("iniv")
 4 class Test_demo():
 5     def test_demo(self):
 6         assert 1==1
 7     def test_demo_1(self):
 8         assert 1==1
 9
10 if __name__ == ‘__main__‘:
11     pytest.main(["-s","-v","test_demo.py"])

运行结果:他会先运行start的在运行iniv的。因为start只是作用于class级别,而iniv是作用于function级别,所以start只需要执行一次,而iniv会有多少用例就运行多少次

3)定义为@pytest.fixture(scope="module")

1 #跟目录下的conftest.py 代码
2 import pytest
3 @pytest.fixture(scope="module")
4 def start():
5     print("\n我是一个装饰module的start")
6
7 @pytest.fixture(scope="function")
8 def iniv():
9     print("\n我是一个装饰function的iniv")
 1 #test_demo.py代码
 2 import pytest
 3 @pytest.mark.usefixtures("start")
 4 @pytest.mark.usefixtures("iniv")
 5 class Test_demo():
 6     def test_demo(self):
 7         assert 1==1
 8     def test_demo_1(self):
 9         assert 1==1
10 @pytest.mark.usefixtures("start")
11 @pytest.mark.usefixtures("iniv")
12 class Test_demo_1():
13     def test_demo(self):
14         assert 1==1
15     def test_demo_1(self):
16         assert 1==1
17
18 if __name__ == ‘__main__‘:
19     pytest.main(["-s","-v","test_demo.py"])

运行结果:他会先运行start的在运行iniv的。因为start只是作用于module级别,而iniv是作用于function级别,虽然我们在test_demo.py里面装饰了两次start,但是因为它是装饰模块的,并且也只有test_demo.py这个一个模块,所以start只需要执行一次,而iniv会有多少用例就运行多少次

4)定义为@pytest.fixture(scope="session")

1 #跟目录下的conftest.py 代码
2 import pytest
3 @pytest.fixture(scope="session",autouse=True)  #居然你是会话级别了,那么直接默认autouse=True就行了,不用调用就自动执行
4 def start():
5     print("\n我是一个装饰session的start")
6
7 @pytest.fixture(scope="function")
8 def iniv():
9     print("\n我是一个装饰function的iniv")
 1 #test_demo.py代码
 2 import pytest
 3 @pytest.mark.usefixtures("iniv")
 4 class Test_demo():
 5     def test_demo(self):
 6         assert 1==1
 7     def test_demo_1(self):
 8         assert 1==1
 9
10 if __name__ == ‘__main__‘:
11     pytest.main(["-s","-v","test_demo.py"])

运行结果:他会先运行start的在运行iniv的。因为start只是作用于session级别,而iniv是作用于function级别,而且我们直接autouse=True,所以不用调用也会执行,而且start只需要执行一次,而iniv会有多少用例就运行多少次

二.多个.py用例文件(这里不展示代码,自己去实践吧)

多个.py用例文件,其实运行结果:

1)function级别:有多少用例就运行多少次。

2)class级别:装饰多少个类上面的,那么一个就运行多少次,例如一个.py用例里面有两个类,都装饰了class级别的,那么就运行两次,如果有两个.py用例文件都有两个类,都装饰了class级别,那么就运行四次。

3)module级别:如果有一个.py用例文件,那么就运行一次module级别,如果有两个.py用例文件,那么就运行两次。

4)session级别:不管有多少个.py用例文件,始终就运行一次

3.设置autouse=True  (这种不建议,因为你不调用它,它始终就会运行)

fixture默认autouse=False


跟目录下的conftest.py 代码
1 import pytest
2 @pytest.fixture(autouse=True)   #启用
3 def start():
4     print("\n全局conftest")
5
6 @pytest.fixture(autouse=False)  #不启用
7 def iniv():
8     print("\n全局iniv")
1 import pytest
2 #@pytest.mark.usefixtures("start")   这里注释掉了
3 class Test_demo():
4     def test_demo(self):
5         assert 1==1
6     def test_demo_1(self):
7         assert 1==1
8 if __name__ == ‘__main__‘:
9     pytest.main(["-s","-v","test_demo.py"])

运行结果:结果还是调用了start

原文地址:https://www.cnblogs.com/hao2018/p/11351125.html

时间: 2024-08-30 15:01:17

pytest-conftest.py作用范围的相关文章

python学习-pytest(二)-conftest.py

一.conftest特点: 1.可以跨.py文件调用,有多个.py文件调用时,可让conftest.py只调用了一次fixture,或调用多次fixture 2.conftest.py与运行的用例要在同一个pakage下,并且有__init__.py文件 3.不需要import导入 conftest.py,pytest用例会自动识别该文件,放到项目的根目录下就可以全局目录调用了,如果放到某个package下,那就在改package内有效,可有多个conftest.py 4.conftest.py

pytest文档5-fixture之conftest.py

前言 前面一篇讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作,但这种是整个脚本全局生效的,如果我想实现以下场景: 用例1需要先登录,用例2不需要登录,用例3需要先登录.很显然这就无法用setup和teardown来实现了.这就是本篇学习的目的,自定义测试用例的预置条件 fixture优势 1.firture相对于setup和teardown来说应该有以下几点优势 命名方式灵活,不局限于setup和teardown这几个命名 conftest.py 配置里可以实现数

pytest框架: fixture之conftest.py

原文地址:https://blog.csdn.net/BearStarX/article/details/101000516 一.fixture优势1.fixture相对于setup和teardown来说应该有以下几点优势: 命名方式灵活,不局限于setup和teardown这几个命名 conftest.py配置 里可以实现数据共享,不需要import就能自动找到一些配置 scope="module"可以实现多个.py跨文件共享前置 scope="session"以

pytest-25-conftest.py作用范围

一个测试工程下是可以有多个conftest.py的文件,一般在工程根目录放一个conftest.py起到全局作用.在不同的测试子目录也可以放conftest.py,作用范围只在该层级以及以下目录生效. conftest层级关系 在web_conf_py项目工程下建两个子项目baidu.blog,并且每个目录下都放一个conftest.py和__init__.py(python的每个package必须要有__init__.py) web_conf_py是工程名称 ├─baidu │ │ conft

pytest_fixture之conftest.py

前面一篇讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作,但这种是整个脚本全局生效的,如果我想实现以下场景: 用例1需要先登录,用例2不需要登录,用例3需要先登录.很显然这就无法用setup和teardown来实现了.这就是本篇学习的目的,自定义测试用例的预置条件 fixture优势 1.firture相对于setup和teardown来说应该有以下几点优势 命名方式灵活,不局限于setup和teardown这几个命名 conftest.py 配置里可以实现数据共享

pytest框架之fixture详细使用

本人之前写了一套基于unnitest框架的UI自动化框架,但是发现了pytest框架之后觉得unnitest太low,现在重头开始学pytest框架,一边学习一边记录,和大家分享,话不多说,那就先从pytest框架的精髓fixture说起吧! 简介: fixture区别于unnitest的传统单元测试(setup/teardown)有显著改进: 1.有独立的命名,并通过声明它们从测试函数.模块.类或整个项目中的使用来激活. 2.按模块化的方式实现,每个fixture都可以互相调用. 3.fixt

(二十五)pytest中conftest使用

conftest中一般放置前置条件,比如登录等 1.在case目录下(有__init__.py文件),新建conftest.py文件 @wiiliam胡阳 2.在用例中添加 @william胡阳 原文地址:https://www.cnblogs.com/wx921308494/p/11966765.html

pytest-24-fixture的作用范围(scope)

fixture作用范围 fixture里面有个scope参数可以控制fixture的作用范围:session > module > class > function fixture(scope="function", params=None, autouse=False, ids=None, name=None): """使用装饰器标记fixture的功能 ** 作者:上海-悠悠 QQ交流群:588402570** 可以使用此装饰器(带或

用 pytest 测试 python 代码

Pytest 是一个比较成熟且功能完备的 Python 测试框架.其提供完善的在线文档,并有着大量的第三方插件和内置帮助,适用于许多小型或大型项目.Pytest 灵活易学,打印调试和测试执行期间可以捕获标准输出,适合简单的单元测试到复杂的功能测试.还可以执行 nose, unittest 和 doctest 风格的测试用例,甚至 Django 和 trial.支持良好的集成实践, 支持扩展的 xUnit 风格 setup,支持非 python 测试.支持生成测试覆盖率报告,支持 PEP8 兼容的