1.conftest.py解释

conftest.py是pytest框架里面一个很重要的东西,它可以在这个文件里面编写fixture,而这个fixture的作用就相当于我们unittest框架里面的setup()和teardown(),虽然pytest框架也有setup()和teardown()但是没必要写在用例里面,直接写在conftests.py里面就好了,这样更灵活,然后pytest框架会自动去找conftest.py这个文件里面的东西。

  1. conftest.py
    1 import pytest
  2. @pytest.fixture()
  3. def login():
  4. print("用例开始执行")
  5. yield #如果有返回值,可以在yield后面直接写返回值就好了,如果有多个直接用(返回值1,返回值2)
  6. print("用例执行完成")

2.目录结构

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

3.用例传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

  1. conftest.py 代码
  2.  
  3. 1 import pytest
  4. @pytest.fixture()
  5. def start_1():
  6. print("\n局部test_case_demo")
  1. test_demo.py 代码
  2.  
  3. 1 import pytest
    2 class Test_demo():
  4. def test_demo(self,start,start_1): #标记代码
  5. assert 1==1
  6.  
  7. if __name__ == '__main__':
  8. pytest.main(["-s","-v","test_demo.py"])

运行结果:

3.test_case_demo_2/conftest.py 和 test_demo_2.py

  1. conftest.py 代码
  2.  
  3. 1 import pytest
  4. @pytest.fixture()
  5. def start_2():
  6. print("\n局部test_case_demo_2")
  1. test_demo_2.py 代码
    1 import pytest
    class Test_demo_2():
  2. def test_demo_2(selfstart,start_1): #标记代码
  3. assert 1==1
  4.  
  5. if __name__ == '__main__':
  6. 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用例会运行失败

4.装饰器usefixtures

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

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

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

  1. 跟目录下的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")
  1. 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就行了,不用调用就自动执行,或者不写autouse=True,直接在随便的,py用例文件里面调用以下就行了
  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用例文件,始终就运行一次

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

fixture默认autouse=False

  1.  
  1. 跟目录下的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

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

  1. conftest.py作用范围

    前言 一个测试工程下是可以有多个conftest.py的文件,一般在工程根目录放一个conftest.py起到全局作用.在不同的测试子目录也可以放conftest.py,作用范围只在该层级以及以下目录 ...

  2. pytest进阶之conftest.py

    前言 前面几篇随笔基本上已经了解了pytest 命令使用,收集用例,finxture使用及作用范围,今天简单介绍一下conftest.py文件的作用和实际项目中如是使用此文件! 实例场景 首先们思考这 ...

  3. pytest文档25-conftest.py作用范围

    前言 一个测试工程下是可以有多个conftest.py的文件,一般在工程根目录放一个conftest.py起到全局作用. 在不同的测试子目录也可以放conftest.py,作用范围只在该层级以及以下目 ...

  4. pytest框架: fixture之conftest.py

    原文地址:https://blog.csdn.net/BearStarX/article/details/101000516 一.fixture优势1.fixture相对于setup和teardown ...

  5. 『德不孤』Pytest框架 — 14、Pytest中的conftest.py文件

    目录 1.conftest.py文件介绍 2.conftest.py的注意事项 3.conftest.py的使用 4.不同位置conftest.py文件的优先级 5.conftest.py中Fixtu ...

  6. pytest自动化3:fixture之conftest.py实现setup

    出处:https://www.cnblogs.com/yoyoketang/p/9390073.html 前言: 前面一篇讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作 ...

  7. pytest 3.fixture介绍一 conftest.py

    前言: 前面一篇pytest2 讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作,但这种是整个脚本全局生效的,如果我想实现以下场景: 用例1需要先登录,用例2不需要登录, ...

  8. pytest文档5-fixture之conftest.py

    前言 前面一篇讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作,但这种是整个脚本全局生效的,如果我想实现以下场景: 用例1需要先登录,用例2不需要登录,用例3需要先登录. ...

  9. pytest执行用例时从conftest.py抛出ModuleNotFoundError:No module named 'XXX'异常的解决办法

    一.问题描述 在项目根目录下执行整个测试用例,直接从conftest.py模块中抛出了ModuleNotFoundError:No module named 'TestDatas'的异常: 二.解决方 ...

  10. Pytest系列(6) - conftest.py的详细讲解

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 什么是conftest.py 可以 ...

随机推荐

  1. js设计模式之实现观察者模式实例代码

    前端界面 html代码 <body> <select name="" id="select"> <option value=&qu ...

  2. set,get,setter

    JS对象属性中get/set与getter/setter是什么 2019-01-18 15:07:44 CHENKAI188 阅读数 686更多 分类专栏: JS修仙系列   版权声明:本文为博主原创 ...

  3. 题解【洛谷P1315】[NOIP2011]观光公交

    题目描述 风景迷人的小城 Y 市,拥有 \(n\) 个美丽的景点.由于慕名而来的游客越来越多,Y 市特意安排了一辆观光公交车,为游客提供更便捷的交通服务. 观光公交车在第 \(0\) 分钟出现在 \( ...

  4. 题解 【洛谷P1035】[NOIP2002普及组]级数求和

    [NOIP2002普及组]级数求和 这个题……用循环也是可以的,不过我写了两种循环的题解,供各位dalao参考!O(∩_∩)O谢谢! for循环版本: #include<bits/stdc++. ...

  5. DSP---TI CCSv5.5.x-Windows安装

    TI CCSv5.5.x(正式版)-Windows版本 国内2013年9月13日首发安装CCSv5.5图示 *请关掉防火墙及杀毒软件进行安装 第一步 第二步 安装程序检测到挂起的重新启动,这可能在安装 ...

  6. 每天进步一点点------CRC码的FPGA实现

    一.CRC码的FPGA实现之一CRC的原理 实验目的 学习用FPGA设计一个数据通信中常用的数据检错模块——循环冗余检验CRC模块,熟悉理解CRC的检错原理. 实验原理 循环冗余检验(CRC)算法原理 ...

  7. Tomcat配置过程

    Tomcat的配置其实还是挺简单的,下面是在Myeclipse中配置 1.首先要在Tomcat官网下载,网址:http://tomcat.apache.org/,然后左侧会有Download,选择你要 ...

  8. leetcode 72.编辑距离(dp)

    链接:https://leetcode-cn.com/problems/edit-distance/submissions/ 设dp[i][j]表示串s1前i个字符变换成串s2前j个字符所需要的最小操 ...

  9. opencv:形态学操作-开闭操作

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  10. Java进阶学习(6)之抽象与接口

    抽象与接口 抽象 抽象函数 表达概念而无法实现具体代码的函数 抽象类 表达概念而无法构造出实体的类 有抽象函数的类也可以有非抽象函数 实现抽象函数 继承自抽象类的子类必须覆盖父类中的抽象函数 抽象 与 ...