zoukankan      html  css  js  c++  java
  • pytest跟我练05-->fixtrue基础之autouse参数

    前言

          青年最主要的任务是学习。—— 朱德
          正值青年的我的任务!!!
    

    一、autouse参数是什么?

    • autouse参数是fixture方法中的其中一个参数;(ps:为了防止有些朋友单独看这篇博文才写了这句)
    • autouse从英文字面意思上来理解是自动使用;
    • autouse参数设置后能自动让范围内的测试方法都执行。

    二、autouse参数应用

    • 2.1 没有使用autouse参数的代码:
    import pytest
    
    @pytest.fixture()
    def setUp():
        print('
    setUp')
        yield
        print('
    tearDown')
    
    def testcase01(setUp):
        print('exectue testcase01')
        assert 1
    def testcase02(setUp):
        print('exectue testcase02')
        assert 1
    
    if __name__=='__main__':
        pytest.main(["-s"])
    
    • 2.2 使用autouse参数的代码:
    import pytest
    
    @pytest.fixture(autouse=True)  #设置为True让autouse生效
    def setUp():
        print('
    setUp')
        yield
        print('
    tearDown')
    
    def testcase01():
        print('exectue testcase01')
        assert 1
    def testcase02():
        print('exectue testcase02')
        assert 1
    
    if __name__=='__main__':
        pytest.main(["-s"])
    

    上述两个实例小结:

    • 读者在执行代码的过程中会发现,两段代码执行的结果是一样的,fixture都生效了;
    • 实例2设置了autouse=True,让我们可以在测试方法中不用一一去添加fixture的名称,方便了不少。
  • 相关阅读:
    将变量名变为字符串
    sqlte3 的约束
    sqlte3 的基本使用4
    sqlite 的基本使用3
    sqlite 的基本使用2
    sqlite 的基本使用1
    TCP和UDP的区别(转)
    Mac 如何优雅的使用Microsoft office
    RGB和HSV颜色空间
    腾讯云视频开发相关参考
  • 原文地址:https://www.cnblogs.com/dream66/p/12508741.html
Copyright © 2011-2022 走看看