2017-05-17 8 views
1

からテストパラメータを注入/渡す方法があります:私は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_namevalue=parameter_new_valueと辞書を渡す関数デコレータを作成するが、私はtests.pyに多くの追加のコードを書かなければならなかった:私は

  • が、私はそれを置くfunction_decoratorロジックを定義し
    1. @私はデコレータ機能

    2. を飾るためにしたいすべての機能上の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) 
      
  • 答えて

    1

    が可能です解決策:

    あなたが別のモジュールからこの方法をあなたのテストを実行します。

    if __name__ == '__main__': 
        testbed_dict = {"ip": "11.111.111.112", 
            "browserType": "Chrome", 
            "port": "4444", 
            "h5_client_url": "https://new_somelink.com/", 
            "h5_username": "new_username", 
            "h5_password": "new_pass"} 
    
        sys.argv.append(testbed_dict) 
    
        from your_tests_module import * 
    
        with open("test.log", "w") as f: 
         runner = unittest.TextTestRunner(f) 
    
         unittest.main(argv=[sys.argv[0]], defaultTest='test_class.test_name', exit=False, testRunner=runner) 
    

    argv=[sys.argv[0]]unittest.main(argv=[sys.argv[0]], defaultTest='test_class.test_name', exit=False, testRunner=runner)に入れることができます。 unittests引数を実際の引数を持つリストに変更します(エラーは発生しません)。このリストの最後には、テストパラメータの新しい値を含む辞書があります。

    [OK]を、今、あなたはこのようになっているはずの関数デコレータ、書き込み:この方法を

    def load_params(system_arguments_list): 
        def decorator(func_to_decorate): 
         @wraps(func_to_decorate) 
         def wrapper(self, *args, **kwargs): 
          kwargs = system_arguments_list[-1] 
    
          return func_to_decorate(self, **kwargs) 
    
         return wrapper 
        return decorator 
    

    そして、このデコレータを使用する:

    @classmethod 
    @load_params(sys.argv) 
    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 
    
    関連する問題