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.
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.
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 ...
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.
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.)
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 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With