Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在模块中的测试文件之间传递py.test夹具_Python_Pytest - Fatal编程技术网

Python 在模块中的测试文件之间传递py.test夹具

Python 在模块中的测试文件之间传递py.test夹具,python,pytest,Python,Pytest,我有一个通用的py.test夹具,我想在同一模块内的不同测试文件中通用。阅读py.test文档后,建议将夹具添加到conftest.py文件中,该文件应使夹具可用于模块中的所有文件。但由于某些原因,我似乎无法让这个通用夹具与我的测试类一起工作 #In conftest.py import pytest @pytest.fixture def mock_data(scope="module"): return ({'number_of_females_1': 14,

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

#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)} 
然后在我的测试类文件中

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_数据
fixture或任何东西

运行此测试用例时出现的错误是:

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
self=
结果=
def运行(自身,结果=无):
原始结果=结果
如果结果为无:
结果=self.defaultTestResult()
startTestRun=getattr(结果为“startTestRun”,无)
如果StartTrun不是无:
startTestRun()
结果。开始测试(自我)
testMethod=getattr(self,self.\u testMethodName)
如果(getattr(self.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu类,“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
getattr(testMethod,“\uuuuu unittest\u skip\uuuuuuuuu”,False)):
#如果类或方法被跳过。
尝试:
跳过为什么=(getattr(self.\uuuuuuuu class\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
或getattr(testMethod,'.'单元测试'.'跳过'.'为什么'.'','')
self.\u addSkip(结果、self、skip\u原因)
最后:
结果:停止测试(自身)
返回
预期\u失败\u方法=getattr(testMethod,
“\uuuuu单元测试\u预期\u失败\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
预期\u失败\u类=getattr(self,
“\uuuuu单元测试\u预期\u失败\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
expecting\u failure=expecting\u failure\u类或expecting\u failure\u方法
结果=_结果(结果)
尝试:
自我。结果=结果
带有output.testPartExecutor(self):
self.setUp()
如果结果成功:
结果。预期失败=预期失败
使用output.testPartExecutor(self,isTest=True):
>testMethod()
E TypeError:test_Base_model()缺少1个必需的位置参数:“mgmt_data”
/home/krishnab/anaconda3/envs/py35_gu/lib/python3.5/unittest/case.py:600:TypeError

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

我找到了答案。问题是我使用的是
Unittest
类型类,而不是
py.test
类型类。从技术上讲,这两个类都可以使用
py.test
,但只有
py.test
类型类可以访问fixture

所以我改变了:

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)

问题解决。

您忘记在参数中包含管理数据。哦,我也尝试过这样做。但错误就不同了。我用新的错误消息更新了OP。这个新消息更让人困惑。那么完全删除类继承就行了吗?pytest现在难道不能将这个类识别为测试用例吗?您仍然需要从某种TestCase类继承,以便让pytest认识到它需要运行这些类believe@frei是的,我认为
pytest
包有自己的TestCase类。导入
pytest
将搜索以
test
开头的所有函数或类,然后pytest从那里开始。