Python:Nosetests:是否可以在运行时在 setup_class 方法中设置属性/标签?

标签 python nosetests

我想设置一个测试套件,其中我将在 setup_class 方法中读取一个 json 文件,并在该 json 文件中提及哪些测试应该运行,哪些测试不应该运行。因此,通过这种方法,我可以通过仅更改 json 文件而不触及测试套件来提及要运行的测试用例。

但是在 setup_class 方法中,当我尝试执行以下操作时:-

class TestCPU:
    testme=False

    @classmethod
    def setup_class(cls):
       cls.test_core.testme=True

    def test_core(self):
       print 'Test CPU Core'
       assert 1

执行以下命令:-

   nosetests -s -a testme

它给出了以下错误:-

   File "/home/murtuza/HWTestCert/testCPU.py", line 7, in setup_class
   cls.test_core.testme=False
   AttributeError: 'instancemethod' object has no attribute 'testme'

那么,是否可以在setup_class的时候设置测试方法的属性呢?

最佳答案

它的定义方式,testme是 TestCPU 类的成员,<unbound method TestCPU.test_core>不知道这个属性。您可以使用 cls.test_core.__dict__['testme']=True 注入(inject) Nose 属性.但是,在您的 setup_class 之前检查属性方法被调用,因此即使设置了属性,您的测试也会被跳过。但是您当然可以在导入时使用属性来装饰您的测试,如下所示:

import unittest

class TestCPU(unittest.TestCase):
    def test_core(self):
       print 'Test CPU Core'
       assert 1

TestCPU.test_core.__dict__['testme']=True

您可能还想尝试 --pdb nosetests 的选项,它将在错误时带出调试器,以便您可以深入了解问题所在。这绝对是我生命中第二喜欢的事情。

关于Python:Nosetests:是否可以在运行时在 setup_class 方法中设置属性/标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21586923/

相关文章:

python - 从 Databricks 上的代码重新启动 Python 解释器?

python - 如何使用 Cython 在项目中运行 Nose 测试

python - 如何使用 nosetests 测试函数在函数内被调用

python nosetests 相当于测试文件中的unittest Testsuite

python - 如何使用nosetests实现多处理的全局setUp()

python - 如何使用 Google Cloud 调度程序 Python api 创建作业

python - Flask 允许多个服务器实例监听同一个端口

python - Jinja2 将 float 转换为小数中间计算

python - 在python列表中找到最小值

python - Nosetests 进行了 0 次测试