Python, unit test - Pass command line arguments to setUp of unittest.TestCase

前端 未结 4 1447
情书的邮戳
情书的邮戳 2020-12-08 03:02

I have a script that acts as a wrapper for some unit tests written using the Python unittest module. In addition to cleaning up some files, creating an output s

4条回答
  •  攒了一身酷
    2020-12-08 03:17

    Well, I want to do the same thing and was going to ask this question myself. I wanted to improve over the following code because it has duplication. It does let me send in arguments to test TestCase however:

    import unittest
    import helpspot
    
    class TestHelpSpot(unittest.TestCase):
        "A few simple tests for HelpSpot"
    
        def __init__(self, testname, path, user, pword):
            super(TestHelpSpot, self).__init__(testname)
            self.hs = helpspot.HelpSpot(path, user, pword)
    
        def test_version(self):
            a = self.hs.version()
            b = self.hs.private_version()
            self.assertEqual(a, b)
    
        def test_get_with_param(self):
            a = self.hs.filter_get(xFilter=1)
    
        def test_unknown_method(self):
            self.assertRaises(helpspot.HelpSpotError, self.hs.private_wuggienorple)
    
    if __name__ == '__main__':
        import sys
        user = sys.argv[1]
        pword = sys.argv[2]
        path = sys.argv[3]
    
        test_loader = unittest.TestLoader()
        test_names = test_loader.getTestCaseNames(TestHelpSpot)
    
        suite = unittest.TestSuite()
        for test_name in test_names:
            suite.addTest(TestHelpSpot(test_name, path, user, pword))
    
        result = unittest.TextTestRunner().run(suite)
        sys.exit(not result.wasSuccessful())
    

提交回复
热议问题