1

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Nathan Tregillus
  • 6,006
  • 3
  • 52
  • 91
  • 1
    That information should be in [the new question](http://stackoverflow.com/questions/30943408/pycharm-errors-for-valid-python-code?lq=1). – jonrsharpe Jun 19 '15 at 17:21

1 Answers1

4

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

Community
  • 1
  • 1
Oleksiy
  • 6,337
  • 5
  • 41
  • 58
  • hold up, it now complains that I am not passing in "self" – Nathan Tregillus Jun 17 '15 at 22:03
  • can you write the code out to fix the first scenario? maybe I am missing what you mean by "remove @staticmethod" – Nathan Tregillus Jun 17 '15 at 22:04
  • added code to show what worked for me. Are you using `generate_scenarios` in some other place as well? – Oleksiy Jun 17 '15 at 23:24
  • ok, if I ignore PyCharms error message, I get results, but I can't leave code like this without removing this warning that is incorrect. any ideas? – Nathan Tregillus Jun 18 '15 at 22:33
  • PyCharm does not realize that you are planning to refer to the the class method before the class is actually created: it thinks that the world is flat: statich or class methods only. But you can define functions within the scope of the class just the same as you can define them anywhere. So sometimes your coding tool is too restrictive, which is good for beginning developers. – Oleksiy Jun 19 '15 at 06:17
  • ah, that is totally me. I have only been developing in Python for 6 months. I'll post this as a seperate PyCharm question. Thanks everyone! – Nathan Tregillus Jun 19 '15 at 16:51