python - py.test 在类下找不到测试

标签 python unit-testing pycharm pytest

我正在尝试创建不基于单元测试的测试类。

这个类下的这个方法

class ClassUnderTestTests:

    def test_something(self):

当您从命令行调用 py.test 或在 PyCharm 中运行此测试时(它在自己的模块上)时,无法检测和运行。

这个

def test_something(self):

可以检测并运行类外的相同方法。

我想将我的测试归类,除非我遗漏了什么,否则我会关注 py.test spec这样做。

环境:Windows 7,将 py.test 设置为测试运行器的 PyCharm。

最佳答案

按照惯例,它会搜索

Test prefixed test classes (without an init method)

例如。

# content of test_class.py
class TestClass:
    def test_one(self):
        x = "this"
        assert 'h' in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, 'check')

    # this works too
    @staticmethod
    def test_three():
        pass

    # this doesn't work
    #@classmethod
    #def test_three(cls):
    #    pass

查看文档:

关于python - py.test 在类下找不到测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20277058/

相关文章:

python - 将 Dataframe 列中的值除以同一列中的前一个值

python - Pycharm 中的 """input().split() in loop"""问题

unit-testing - 单元测试自定义StyleCop规则

RequireJS/AMD 模块中的 JavaScript 代码覆盖率

java - 如何模拟 DriverManager.getConnection(...)?

python-3.x - Tkinter 复制到剪贴板在 PyCharm 中不起作用

python - PyTest conftest.py 错误地显示了 PyCharm 中的目录 os.getcwd()

python - 我可以让 Celery 重新排队任务吗?

python - 使用 strptime (Python) 时出现错误消息

python - 如何应用函数可以使用 python-pandas 测试数据框中列表中的元素是否?