Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.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 如何使pytest忽略名为TestSomething的类?_Python_Pytest - Fatal编程技术网

Python 如何使pytest忽略名为TestSomething的类?

Python 如何使pytest忽略名为TestSomething的类?,python,pytest,Python,Pytest,我正在开发一种测试框架,它碰巧有一个名为TestSomething的类。我意识到我的测试失败了,因为pytest一导入就将这些类视为“需要实例化和运行的东西!”。这绝对行不通 import pytest from package import TestSomethingClass 是否可以直接从pytest文件导入此类类?或者我应该通过固定装置间接地使用它们吗 同样的问题也适用于异常,因为我需要做如下操作 with pytest.raises(TestsSomethingError): 显式

我正在开发一种测试框架,它碰巧有一个名为
TestSomething
的类。我意识到我的测试失败了,因为pytest一导入就将这些类视为“需要实例化和运行的东西!”。这绝对行不通

import pytest
from package import TestSomethingClass
是否可以直接从pytest文件导入此类类?或者我应该通过固定装置间接地使用它们吗

同样的问题也适用于异常,因为我需要做如下操作

with pytest.raises(TestsSomethingError):
显式禁用 您可以允许pytest忽略这个特定的类,因为它以单词
Test
开头,在冲突的类中将
\uuuuuuuuuuuuu
标志设置为
False

class TestSomethingClass(object):
    __test__ = False

    def test_class_something(self, object):
        pass
功能:


配置文件 我们还可以在您的
pytest.ini
文件中完全更改约定,并忽略以单词
Example
开头的所有类名。但我不建议这样做,因为我们可能会遇到意想不到的后果

#pytest.ini file
[pytest]
python_classes = !Example

Pytest约定
  • 类外的测试前缀测试函数或方法

  • 测试前缀测试类内的测试前缀测试函数或方法(无init方法)


来源:

完美!我最终使用了配置文件选项,这避免了在代码库中注入与单元测试相关的代码。谢谢:)