在模块中的测试文件之间传递 py.test 固定装置 [英] passing a py.test fixture between test files in a module

查看:24
本文介绍了在模块中的测试文件之间传递 py.test 固定装置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用的 py.test 固定装置,我想在同一模块内的不同测试文件中通用它.阅读 py.test 文档后,建议将夹具添加到 conftest.py 文件中,该文件应该使该夹具可用于模块中的所有文件.但出于某种原因,我似乎无法让这个通用夹具与我的测试类一起使用.

I have a common py.test fixture that I want to use generically in different test files within the same module. After reading the py.test documentation, the suggestion was to add the fixture to a conftest.py file which should make the fixture available to all files in the module. But for some reason, I can't seem to get this common fixture to work with my test class.

#In conftest.py

import pytest

@pytest.fixture
def mock_data(scope="module"):
    return ({'number_of_females_1': 14,
             'number_of_females_2': 3,
             'number_of_females_3': 19,
             'number_of_males_1': 37)} 

然后在我的测试类文件中

Then in my test class file I have

Import pytest
from unittest import TestCase    

@pytest.mark.usefixtures('mgmt_data')
class Test_param_sweeps(TestCase):

    def test_Base_model(self, mgmt_data):
        from pyugend.pyugend.Models import Base_model
        t = Base_model(**mgmt_data)
        assert isinstance(t, Base_model)

文档说我不必导入 mgmt_data 夹具或任何东西.

The documentation said that I did not have to import the mgmt_data fixture or anything.

我在运行这个测试用例时得到的错误是:

The error I get when running this test case is:

self = <pyugend.tests.test_param_sweeps.Test_param_sweeps testMethod=test_Base_model>
result = <TestCaseFunction 'test_Base_model'>

    def run(self, result=None):
        orig_result = result
        if result is None:
            result = self.defaultTestResult()
            startTestRun = getattr(result, 'startTestRun', None)
            if startTestRun is not None:
                startTestRun()

        result.startTest(self)

        testMethod = getattr(self, self._testMethodName)
        if (getattr(self.__class__, "__unittest_skip__", False) or
            getattr(testMethod, "__unittest_skip__", False)):
            # If the class or method was skipped.
            try:
                skip_why = (getattr(self.__class__, '__unittest_skip_why__', '')
                            or getattr(testMethod, '__unittest_skip_why__', ''))
                self._addSkip(result, self, skip_why)
            finally:
                result.stopTest(self)
            return
        expecting_failure_method = getattr(testMethod,
                                           "__unittest_expecting_failure__", False)
        expecting_failure_class = getattr(self,
                                          "__unittest_expecting_failure__", False)
        expecting_failure = expecting_failure_class or expecting_failure_method
        outcome = _Outcome(result)
        try:
            self._outcome = outcome

            with outcome.testPartExecutor(self):
                self.setUp()
            if outcome.success:
                outcome.expecting_failure = expecting_failure
                with outcome.testPartExecutor(self, isTest=True):
>                   testMethod()
E                   TypeError: test_Base_model() missing 1 required positional argument: 'mgmt_data'

/home/krishnab/anaconda3/envs/py35_gu/lib/python3.5/unittest/case.py:600: TypeError

我不确定错误是什么?它说我缺少一个位置参数,但 mgmt_data() 不接受任何参数,而 Base_model() 类只接受一个参数,即 **mgmt_data.

I am not sure that the error is? It says that I am missing a positional argument, but mgmt_data() does not take any arguments and the Base_model() class takes only one argument which is the **mgmt_data.

推荐答案

我找到了答案.问题是我使用的是 Unittest 类型类而不是 py.test 类型类.从技术上讲,两者都可以与 py.test 一起使用,但只有 py.test 类型的类可以访问设备.

I figured out the answer. The issue was that I was using a Unittest type class instead of a py.test type class. Technically both can work with py.test but only py.test type classes can access fixtures.

所以我只是改变了:

from unittest import TestCase    

@pytest.mark.usefixtures('mgmt_data')
class Test_param_sweeps(TestCase):

在OP中,如下:

import pytest

@pytest.mark.usefixtures('mgmt_data')
class Test_param_sweeps:

    def test_Base_model(self, mgmt_data):
        from pyugend.pyugend.Models import Base_model
        t = Base_model(**mgmt_data)
        assert isinstance(t, Base_model)

问题解决了.

这篇关于在模块中的测试文件之间传递 py.test 固定装置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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