use class method in nose parameterize.expand call

吃可爱长大的小学妹 提交于 2019-12-11 10:48:17

问题


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: I want my scenario generate_scenarios method to be within the test class that is calling it.


回答1:


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)

And here is how I ran it:

$ nosetests stackoverflow.py -v
test_scenario_0_this_is_my_test (stackoverflow.MyUnitTestClass) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.002s

OK

You may also want to review Python decorator as a staticmethod



来源:https://stackoverflow.com/questions/30874795/use-class-method-in-nose-parameterize-expand-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!