簡體   English   中英

Python,單元測試 - 將命令行參數傳遞給 unittest.TestCase 的設置

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

我有一個腳本,它充當使用 Python unittest模塊編寫的某些單元測試的包裝器。 除了清理一些文件、創建輸出流和生成一些代碼之外,它還使用以下命令將測試用例加載到套件中

unittest.TestLoader().loadTestsFromTestCase()

我已經在使用optparse來提取幾個用於確定輸出位置、是否重新生成代碼以及是否進行一些清理的命令行參數。 我還想傳遞一個配置變量,即端點 URI,以在測試用例中使用。

我意識到我可以將OptionParser添加到 TestCase 的 setUp 方法中,但我想改為將選項傳遞給setUp 這可以使用loadTestsFromTestCase()嗎? 我可以遍歷返回的TestSuiteTestCases ,但是我可以手動調用TestCases上的 setUp 嗎?

**編輯**我想指出的是,我能夠將參數傳遞給setUp如果我遍歷測試和呼叫setUp手動像:

(options, args) = op.parse_args()
suite = unittest.TestLoader().loadTestsFromTestCase(MyTests.TestSOAPFunctions)
for test in suite:
    test.setUp(options.soap_uri)

但是,我為此使用了xmlrunner ,它的 run 方法將TestSuite作為參數。 我假設它將運行 setUp 方法本身,所以我需要XMLTestRunner可用的參數。

好吧,我想做同樣的事情,並且要自己問這個問題。 我想改進以下代碼,因為它有重復。 但是,它確實讓我發送參數來測試 TestCase:

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())
if __name__ == '__main__':
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-z", "--zebra",
                      action="store_true", dest="zebra", default=False,
                      help="run like a zebra")    


    (options, args) = parser.parse_args()

    if options.zebra:
        zebrafy()


    # remove our args because we don't want to send them to unittest
    for x in sum([h._long_opts+h._short_opts for h in parser.option_list],[]):
        if x in sys.argv:
            sys.argv.remove(x)


    unittest.main()

我絕對會建議不要像這樣將參數傳遞給 setUp; setUp 旨在在運行測試時隱式調用,因此您不應像這樣顯式調用它。

解決此問題的一種方法是將需要設置的值設置為環境變量或全局可訪問“上下文”模塊中的值,這將允許測試用例根據需要訪問它們。 我會去使用環境變量,因為它在運行測試方面更靈活(然后你不再依賴命令行參數)。

如果您在 init 方法中定義屬性,那么您可以像這樣簡單地將它們全部傳遞到構造函數中。

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.path = path
        self.user = user
        self.pword = pword
....
....
....


if __name__ == '__main__':
    True

    suite = unittest.TestSuite()
    suite.addTest(TestHelpSpot("test_version", path, user, pword))    

    unittest.TextTestRunner().run(suite)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM