Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parameterize python unittest setUp method?

I'm trying to run same test cases with different setUp methods. I've tried using nosetests and parameterized but it seems like it doesn't support parameterizing setUp methods. Here is an example of what I'm trying to do:

...
from nose_parameterized import parameterized

class Example(unittest.TestCase):

    @parameterized.expand(['device1', 'device2'])
    def setUp(self, device):
        desired_caps = {}
        desired_caps['key1'] = device
        desired_caps['key2'] = 'constant value'

    self.driver = webdriver.Remote(url, desired_caps)

    def tearDown(self):
        self.driver.quit()

    def test_app_launch(self):
        # assert something

The error is: TypeError: setUp() takes exactly 2 arguments (1 given).

Is there some other way how to parameterize setUp method? I also looked into nosetests generators but it doesn't seem to be the way to go either.

like image 728
finspin Avatar asked Feb 24 '15 12:02

finspin


People also ask

What is setUp in unittest Python?

setUp allows us to write preparation code that is run for all of our tests in a TestCase subclass. Note: If you have multiple test files with TestCase subclasses that you'd like to run, consider using python -m unittest discover to run more than one test file.

How do you pass a command line argument in unittest Python?

So the way I use to handle the command line arguments can be summarized as: Refactor your program to have the arguments parsing as a function. Refactor your program to handle the arguments parsing differently when doing unit testing. In the unit tests, set the arguments and pass them directly to the functions under ...

How do you parameterize in Python?

Hence, we can use same name for the parameter present in function definition and any other variable outside function. In below example, we have used a parameter 'num'. Outside the function also, we have defined a variable with name 'num'. Parameter 'num' will take value provided via argument on function call.

Is PyUnit the same as unittest?

PyUnit is an easy way to create unit testing programs and UnitTests with Python. (Note that docs.python.org uses the name "unittest", which is also the module name.)


1 Answers

So my approach would be setting up a base test that contains all the tests that the devices have to pass. Then you have to deviceTests that inherit from that baseTest with their own additional setUps which would be device specific.

# this is the base test. Everything that is not specific to the device is set up here. It also contains all the testCases.
import unittest
class deviceTest( unittest.TestCase ):

  def setUp( self ):
    '''
    General setUp here
    '''
    self.desired_caps = {}
    self.desired_caps['key2'] = 'constant value'

  def testWorkflow( self ):
    '''
    Here come the tests that the devices have to pass
    '''

class device1Test( deviceTest ):

  def setUp( self ):
    '''
    device1 specific setup
    '''
    #also run general setUp    
    deviceTest.setUp( self )
    self.desired_caps['key1'] = device
    self.driver = webdriver.Remote(url, desired_caps)


class device2Test( deviceTest ):

  def setUp( self ):
    '''
    device2 specific setup
    '''
    #also run general setUp
    deviceTest.setUp( self )
    self.desired_caps['key1'] = device
    self.driver = webdriver.Remote(url, desired_caps)


if __name__ == '__main__':
  suite = unittest.defaultTestLoader.loadTestsFromTestCase( device1Test )
  suite.addTest( unittest.defaultTestLoader.loadTestsFromTestCase(device2Test ) )
  unittest.TextTestRunner( verbosity = 2 ).run( suite )
like image 187
Philipp Avatar answered Sep 20 '22 14:09

Philipp