Python ユニット テストを含むディレクトリがあります。各ユニット テスト モジュールは、test_*.pyという形式です。私はall_test.pyというファイルを作成しようとしています。これは、ご想像のとおり、前述のテスト形式のすべてのファイルを実行し、結果を返します。これまでに 2 つの方法を試しましたが、どちらも失敗しました。2 つの方法を紹介します。これを正しく実行する方法を知っている人がいれば幸いです。
最初の勇敢な試みとして、私は「ファイル内のすべてのテスト モジュールをインポートして、このunittest.main()
doodad を呼び出せば、動作するはずだ」と考えました。しかし、それは間違いでした。
import glob
import unittest
testSuite = unittest.TestSuite()
test_file_strings = glob.glob('test_*.py')
module_strings = [str[0:len(str)-3] for str in test_file_strings]
if __name__ == "__main__":
unittest.main()
これは機能しませんでしたが、次のような結果になりました:
$ python all_test.py
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
2 回目の試みとして、このテスト全体をもっと「手動」で実行してみようと思いました。そこで、以下のように実行してみました。
import glob
import unittest
testSuite = unittest.TestSuite()
test_file_strings = glob.glob('test_*.py')
module_strings = [str[0:len(str)-3] for str in test_file_strings]
[__import__(str) for str in module_strings]
suites = [unittest.TestLoader().loadTestsFromName(str) for str in module_strings]
[testSuite.addTest(suite) for suite in suites]
print testSuite
result = unittest.TestResult()
testSuite.run(result)
print result
#Ok, at this point I have a result
#How do I display it as the normal unit test command line output?
if __name__ == "__main__":
unittest.main()
これもうまくいきませんでしたが、かなり近いようです!
$ python all_test.py
<unittest.TestSuite tests=[<unittest.TestSuite tests=[<unittest.TestSuite tests=[<test_main.TestMain testMethod=test_respondes_to_get>]>]>]>
<unittest.TestResult run=1 errors=0 failures=0>
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
何らかのスイートがあり、結果を実行できるようです。 しかないと表示されているのが少し心配です。 であるrun=1
べきだと思いますrun=2
が、これは進行中です。しかし、結果を main に渡して表示するにはどうすればよいでしょうか。または、基本的にこれを動作させて、このファイルを実行し、その際にこのディレクトリ内のすべてのユニット テストを実行できるようにするにはどうすればよいでしょうか。