在鼻子parameterize.expand调用中使用类方法 [英] use class method in nose parameterize.expand call
问题描述
我有一个生成器方法正在构建大量的测试标准,我知道我可以使非类方法被调用,但是我宁愿将参数构建方法作为我的测试类的一部分.有办法吗?
I have a generator method that is building a large set of test criteria, I know i can make a non class method to be called, but I much rather have the parameter building method be part of my test class. is there a way to do this?
这是我要做什么的简单描述:
here is a simple description of what I want to do:
class MyUnitTestClass(TestCase):
@staticmethod
def generate_scenarios():
yield ('this_is_my_test', 1, 2)
@parameterized.expand(generate_scenarios())
def test_scenario(self, test_name, input, expected_output)
self.assertEquals(input+input, expected_output)
现在我要执行以下操作:
right now I have do do the following:
def generate_scenarios():
yield ('this_is_my_test', 1, 2)
class MyUnitTestClass(TestCase):
@parameterized.expand(generate_scenarios())
def test_scenario(self, test_name, input, expected_output)
self.assertEquals(input+input, expected_output)
TL; DR::我希望我的方案generate_scenarios方法在调用它的测试类中.
TL;DR: I want my scenario generate_scenarios method to be within the test class that is calling it.
推荐答案
只需删除@staticmethod
,它就可以工作. generate_scenarios
仅仅是在类中定义的一个函数,您将获得适合您的参数化扩展功能:
Just remove @staticmethod
and it should work. generate_scenarios
would be just a function defined within class and you will get parametirised expansion working for you:
from unittest import TestCase
from nose_parameterized import parameterized
class MyUnitTestClass(TestCase):
def generate_scenarios():
yield ('this_is_my_test', 1, 2)
@parameterized.expand(generate_scenarios())
def test_scenario(self, test_name, input, expected_output):
self.assertEquals(input+input, expected_output)
这是我的运行方式:
$ nosetests stackoverflow.py -v
test_scenario_0_this_is_my_test (stackoverflow.MyUnitTestClass) ... ok
----------------------------------------------------------------------
Ran 1 test in 0.002s
OK
您可能还需要查看 Python装饰器作为静态方法
这篇关于在鼻子parameterize.expand调用中使用类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!