Search code examples
nosenose-parameterized

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


Solution

  • 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