2016-08-09 8 views
0

私はunit test in DjangoのためにassertRaisesを使用しています。私がテストしたいassertオプションパラメータ付きメソッドのレイアース

例方法:

def example_method(var, optional_var=None): 
    if optional_var is not None: 
     raise ExampleException() 

私の試験方法:

def test_method(self): 
    self.assertRaises(ExampleException, example_method, ???) 

私は例外を発生させるために引数を渡す必要がありますどのように?それを行うには

答えて

1

二つの方法:

  1. だけで問題になっているようしかし置く引数

    def test_method(self):   
        self.assertRaises(ExampleException, example_method, "some_var", 
             optional_var="not_none") 
    
  2. with付:

    などがPython Docsで説明

    def test_method(self): 
        with self.assertRaises(ExampleException): 
         example_method("some_var", "not_none") 
    
関連する問題