からテストパラメータを注入/渡す方法があります:私はtests.pyで、次のテストクラス考えてみましょう外
class MyTest(unittest.TestCase):
@classmethod
def setUpClass(cls, ip="11.111.111.111",
browserType="Chrome",
port="4444",
h5_client_url="https://somelink.com/",
h5_username="username",
h5_password="pass"):
cls.driver = get_remote_webdriver(ip, port, browserType)
cls.driver.implicitly_wait(30)
cls.h5_client_url = h5_client_url
cls.h5_username = h5_username
cls.h5_password = h5_password
@classmethod
def tearDownClass(cls):
cls.driver.quit()
def test_01(self):
# test code
def test_02(self):
# test code
...
def test_N(self):
# test code
すべての私のテスト(test_Nにtest_01)パラメータを使用し、提供はsetUpClass
。私はそれらのパラメータの新しい値を注入することができる場合
ip="11.111.111.111",
browserType="Chrome",
port="4444",
h5_client_url="https://somelink.com/",
h5_username="username",
h5_password="pass"
は、だから私は疑問に思う:これらのパラメータはデフォルト値があります。そして、私は別のpythonスクリプトからそれをやりたいので、テストのコードに変更やほんのわずかな変更はありません。
注:私はバッチ/シェルコマンドによって、私のテストを実行し、ログファイルにテストの出力を保存したい(そのログファイルに標準出力をリダイレクトする)
一つは、私がやったと思うがしましたkey=parameter_name
とvalue=parameter_new_value
と辞書を渡す関数デコレータを作成するが、私はtests.pyに多くの追加のコードを書かなければならなかった:私は
- @私はデコレータ機能
を飾るためにしたいすべての機能上のfunction_decoratorアノテーションは、パラメータとしてその辞書を必要とするので、私はそのような何かに見えること、メインをした:ここでは
if __name__ == '__main__': # terminal command to run tests should look like this /it is executed by the run-test PARROT command/ # python [this_module_name] [dictionary_containing_parameters] [log_file.log] *[tests] parser = argparse.ArgumentParser() # add testbeds_folder as scripts' first parameter, test_log_file as second and tests as the rest parser.add_argument('dictionary_containing_parameters') parser.add_argument('test_log_file') parser.add_argument('unittest_args', nargs='*') args = parser.parse_args() dictionary_containing_parameters = sys.argv[1] test_log_file = sys.argv[2] # removes the "dictionary_containing_parameters" and "test_log_file" from sys.args - otherwise an error occurs unittest TestRunner sys.argv[1:] = args.unittest_args # executes the test/tests and save the output to the test_log_file with open(test_log_file, "w") as f: runner = unittest.TextTestRunner(f) unittest.main(defaultTest=sys.argv[1:], exit=False, testRunner=runner)