py.test 方法每次运行只执行一次 [英] py.test method to be executed only once per run

查看:369
本文介绍了py.test 方法每次运行只执行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im new to pytest(and python). In have set of things to be executed only once before all my tests(ex:- starting android emulator, creating appium driver, instantiating all my page classes so that I can use them in tests). Btw, I have my tests in multiple classes. After bit of reading I thought @pytest.yield_fixture(scope="session", autouse=True) would do the trick.. but thats not what I see.. Please see below example..

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 中设置测试框架的正确方法吗?

What am I missing ?? Why @pytest.yield_fixture(scope="session", autouse=True) is being executed per test class and why tear down is happen after complete test run? Over all, is this right way of setting up test framework in Pytest ?

推荐答案

发生这种情况是因为您在一个类中定义了设备,然后将其子类化,导致设备被定义两次.

This happens because you define your fixtures inside a class and then subclass it, causing the fixtures to be defined twice.

相反,您应该只将夹具定义为普通函数,并且如果您想使用类对测试进行分组,或者将您的测试也更改为函数,或者不从任何基类继承.

Instead you should just define the fixtures as normal functions, and either change your tests to be functions as well, or not inherit from any base class if you want to use classes for grouping tests.

这篇关于py.test 方法每次运行只执行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆