繁体   English   中英

py.test方法每次运行只能执行一次

[英]py.test method to be executed only once per run

我是pytest(和python)的新手。 在所有测试之前,只能执行一次操作(例如:启动android模拟器,创建appium驱动程序,实例化所有页面类,以便可以在测试中使用它们)。 顺便说一句,我在多个课程中进行测试。 经过@pytest.yield_fixture(scope="session", autouse=True)阅读,我认为@pytest.yield_fixture(scope="session", autouse=True)可以解决问题..但这不是我所看到的。

import pytest

class TestBase():
    @pytest.yield_fixture(scope="session", autouse=True)
    def fixture_session(self):
        # start emulator, create driver and instantiate all page
        # classes with driver create above
        print "\n in fixture_session! @session "   
        yield
        # tear down
        print "in fixture_session after yield @session"
    @pytest.yield_fixture(scope="module", autouse=True)
    def fixture_module(request):
        print 'in fixture_module @module'
        # tear down
        yield
        print "in fixture_module after yield @module"

class TestOne(TestBase):
    def test_a(self):
        # write test with page objects created in TestBase
        print "in test_a of TestOne"  
    def test_b(self):
        print "in test_b of TestOne"

class TestTwo(TestBase):
    def test_a(self):
        print "in test_a of TestTwo"

    def test_b(self):
        print "in test_b of TestTwo"

运行这个给

test_base.py
 in fixture_session! @session
in fixture_module @module
in test_a of TestOne
.in test_b of TestOne
.
 in fixture_session! @session
in fixture_module @module
in test_a of TestTwo
.in test_b of TestTwo
.in fixture_module after yield @module
in fixture_module after yield @module
in fixture_session after yield @session
in fixture_session after yield @session

我想念什么?? 为什么每个测试类都执行@pytest.yield_fixture(scope="session", autouse=True) ,为什么在完整的测试运行后发生拆卸? 总而言之,这是在Pytest中建立测试框架的正确方法吗?

发生这种情况是因为您在类中定义了灯具,然后对其进行了子类化,导致灯具被定义了两次。

相反,您应该只将固定装置定义为正常功能,或者将测试也更改为功能,或者如果要使用类将测试分组,则不要继承任何基类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM