• pytest 用例之间共享变量


    1、在setup中创建不可变变量。各个用例和teardown都可以使用,但是不能修改变量。

      示例如下

    #coding:utf-8
    
    import pytest
    
    class Test_share_var(object):
        def setup_class(self):
            print("\nhere is setup_class")
            self.driver = "driver_setup" # setup中创建变量
        def test_step1(self):
            print("\nhere is test_step1")
            self.driver = "driver_step1" # 用例中修改变量
        def test_step2(self):
            print("\nhere is test_step2")
            print("driver:{}".format(self.driver)) # 用例中引用变量
        def teardown_class(self):
            print("\nhere is teardown_class")
            print("driver:{}".format(self.driver)) # teardown中引用变量
    
    if __name__=="__main__":
        args = ["share_var.py::Test_share_var","-sv"]
        pytest.main(args)

    结果如下:

    python share_var.py
    =================================================================== test session starts ====================================================================
    platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\wangwuhui\AppData\Local\Programs\Python\Python38\python.exe
    cachedir: .pytest_cache
    metadata: {'Python': '3.8.10', 'Platform': 'Windows-10-10.0.18363-SP0', 'Packages': {'pytest': '6.2.5', 'py': '1.11.0', 'pluggy': '1.0.0'}, 'Plugins': {'allure-pytest': '2.9.45', 'dependency': '0.5.1', 'html': '3.1.1', 'metadata': '1.11.0', 'mock': '3.7.0', 'rerunfailures': '10.2'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_301'}
    rootdir: C:\Users\wangwuhui\Desktop
    plugins: allure-pytest-2.9.45, dependency-0.5.1, html-3.1.1, metadata-1.11.0, mock-3.7.0, rerunfailures-10.2
    collected 2 items
    
    share_var.py::Test_share_var::test_step1
    here is setup_class
    
    here is test_step1
    PASSED
    share_var.py::Test_share_var::test_step2
    here is test_step2
    driver:driver_setup # 可以引用变量。可以看到值为driver_setup,说明test_step1修改变量值没有成功。
    PASSED
    here is teardown_class
    driver:driver_setup # 可以引用变量。
    
    
    ==================================================================== 2 passed in 0.04s =====================================================================

    2、在用例中创建不可变变量,不能共享给其它用例使用。

      示例如下

    #coding:utf-8
    
    import pytest
    
            
    class Test_share_var1(object):
        def setup_class(self):
            print("\nhere is setup_class")
        def test_step1(self):
            print("\nhere is test_step1")
            self.var_step1 = "var_step1" # 在用例中创建变量
        def test_step2(self):
            print("\nhere is test_step2")
            print("var_step1:{}".format(self.var_step1)) # 在用例中引用变量
        def teardown_class(self):
            print("\nhere is teardown_class")
            print("var_step1:{}".format(self.var_step1))
    
    if __name__=="__main__":
        args = ["share_var.py::Test_share_var1","-sv"]
        pytest.main(args)

    结果如下:

    python share_var.py
    =================================================================== test session starts ====================================================================
    platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\wangwuhui\AppData\Local\Programs\Python\Python38\python.exe
    cachedir: .pytest_cache
    metadata: {'Python': '3.8.10', 'Platform': 'Windows-10-10.0.18363-SP0', 'Packages': {'pytest': '6.2.5', 'py': '1.11.0', 'pluggy': '1.0.0'}, 'Plugins': {'allure-pytest': '2.9.45', 'dependency': '0.5.1', 'html': '3.1.1', 'metadata': '1.11.0', 'mock': '3.7.0', 'rerunfailures': '10.2'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_301'}
    rootdir: C:\Users\wangwuhui\Desktop
    plugins: allure-pytest-2.9.45, dependency-0.5.1, html-3.1.1, metadata-1.11.0, mock-3.7.0, rerunfailures-10.2
    collected 2 items
    
    share_var.py::Test_share_var1::test_step1
    here is setup_class
    
    here is test_step1
    PASSED
    share_var.py::Test_share_var1::test_step2
    here is test_step2
    FAILED
    here is teardown_class
    
    share_var.py::Test_share_var1::test_step2 ERROR
    
    ========================================================================== ERRORS ==========================================================================
    _____________________________________________________ ERROR at teardown of Test_share_var1.test_step2 ______________________________________________________
    
    self = <class 'share_var.Test_share_var1'>
    
        def teardown_class(self):
            print("\nhere is teardown_class")
    >       print("var_step1:{}".format(self.var_step1))
    E       AttributeError: type object 'Test_share_var1' has no attribute 'var_step1'
    
    share_var.py:30: AttributeError
    ========================================================================= FAILURES =========================================================================
    ________________________________________________________________ Test_share_var1.test_step2 ________________________________________________________________
    
    self = <share_var.Test_share_var1 object at 0x000001B8B0274070>
    
        def test_step2(self):
            print("\nhere is test_step2")
    >       print("var_step1:{}".format(self.var_step1))
    E       AttributeError: 'Test_share_var1' object has no attribute 'var_step1'
    
    share_var.py:27: AttributeError
    ================================================================= short test summary info ==================================================================
    FAILED share_var.py::Test_share_var1::test_step2 - AttributeError: 'Test_share_var1' object has no attribute 'var_step1'
    ERROR share_var.py::Test_share_var1::test_step2 - AttributeError: type object 'Test_share_var1' has no attribute 'var_step1'
    =========================================================== 1 failed, 1 passed, 1 error in 0.51s ===========================================================

    报错信息

    AttributeError: type object 'Test_share_var1' has no attribute 'var_step1'说明,test_step1中创建的变量不能共享给
    在test_step2中引用。

    3、在setup中创建可变变量。各个用例和teardown都可以使用,还能修改变量。

      示例如下

    #coding:utf-8
    
    import pytest
    
    class Test_share_var3(object):
        def setup_class(self):
            print("\nhere is setup_class")
            self.inner_var = {"var1":"a","var2":"b"} # 在setup中创建可变变量
        def test_step1(self):
            print("\nhere is test_step1")
            self.inner_var['var1']="var_step1" # 在test_step1中修改可变变量
        def test_step2(self):
            print("\nhere is test_step2")
            print("var1:{}".format(self.inner_var['var1'])) # 在test_step2中引用可变变量
            print("var2:{}".format(self.inner_var['var2']))
        def teardown_class(self):
            print("\nhere is teardown_class")
            print("var1:{}".format(self.inner_var['var1'])) # 在teardown中引用可变变量
            print("var2:{}".format(self.inner_var['var2']))
    
    if __name__=="__main__":
        args = ["share_var.py::Test_share_var3","-sv"]
        pytest.main(args)

    结果如下:

    python share_var.py
    =================================================================== test session starts ====================================================================
    platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\wangwuhui\AppData\Local\Programs\Python\Python38\python.exe
    cachedir: .pytest_cache
    metadata: {'Python': '3.8.10', 'Platform': 'Windows-10-10.0.18363-SP0', 'Packages': {'pytest': '6.2.5', 'py': '1.11.0', 'pluggy': '1.0.0'}, 'Plugins': {'allure-pytest': '2.9.45', 'dependency': '0.5.1', 'html': '3.1.1', 'metadata': '1.11.0', 'mock': '3.7.0', 'rerunfailures': '10.2'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_301'}
    rootdir: C:\Users\wangwuhui\Desktop
    plugins: allure-pytest-2.9.45, dependency-0.5.1, html-3.1.1, metadata-1.11.0, mock-3.7.0, rerunfailures-10.2
    collected 2 items
    
    share_var.py::Test_share_var3::test_step1
    here is setup_class
    
    here is test_step1
    PASSED
    share_var.py::Test_share_var3::test_step2
    here is test_step2
    var1:var_step1 # var1的值从a变为var_step1,说明用例可以引用和修改共享的可变变量
    var2:b
    PASSED
    here is teardown_class
    var1:var_step1 # 说明teardown可以引用共享的可变变量
    var2:b
    
    
    ==================================================================== 2 passed in 0.06s =====================================================================
  • 相关阅读:
    (转载)delphi文件流
    一个不敢妄称自己是程序员的半拉子编码员的随想
    Xamarin.iOS使用极光JPush进行推送
    Xamarin.IOS问题记录——项目属性里IOS Bundle Signing 配置文件选项没有对应的配置文件选择
    Xamarin问题记录
    Unity3D笔记
    C#Xml To Class生成器
    WPF Mahapps.Metro 设置主题样式
    WPF画N角芒星,正N角星
    WPFPath素材
  • 原文地址:https://www.cnblogs.com/superbaby11/p/16163934.html
Copyright © 2020-2023  润新知