Reputation: 3910
I'm writing a test script in Python subclassing unittest.TestCase
. There're multiple test methods starting with test_
.
What I want to do know is to invoke different test methods according to input arguments, like trial test.py -option
. I wonder if there's a way to do this in the unittest framework. Thanks a lot.
Upvotes: 2
Views: 183
Reputation: 3146
Yes you can! Straight from the unittest documentation on organizing your test code
Assume you have a TestCase that looks like this guy:
import unittest
class WidgetTestCase(unittest.TestCase):
def setUp(self):
self.widget = Widget('The widget')
def tearDown(self):
self.widget.dispose()
self.widget = None
def test_default_size(self):
self.assertEqual(self.widget.size(), (50,50),
'incorrect default size')
def test_resize(self):
self.widget.resize(100,150)
self.assertEqual(self.widget.size(), (100,150),
'wrong size after resize')
Now you can do something like this in your TestSuite setup/run code:
def suite():
suite = unittest.TestSuite()
suite.addTest(WidgetTestCase('test_default_size'))
suite.addTest(WidgetTestCase('test_resize'))
return suite
Obviously, you can use argparse to set your own options and customize the structure to your needs. Check out the documentation on the Organizing Your Test Code section. That example is just the most explicit one, there are plenty of other ways to test this cat.
Upvotes: 2