2017-02-24 21 views
2

私は初心者です。私は本当にこのエラーが何を指しているのか分かりません。どんな助けでも大歓迎です。 コードは次のように特定の国の人々の税金を計算することになっている: 年間収入を:0から1000attributeError( "'_ AssertRaisesContext'オブジェクトに属性 '例外'"がありません ")、

税率:0%

年間収入:1001 - 万

税率:10%

年間収入:10001 - 20200

税率:15%

年間収入:20201 - 30750

税率:20%

年間収入:30751 - 50,000

税率:25%

年間収入:50,000以上

税率:30%

マイコード:

def calculate_tax(pDict): 
    if type(pDict) is dict: 
    try: 
     length = len(pDict) 
     count = 0 
     #decleration of new updated dictionary 
     dict_list = {} 
     while count<length: 
     #calculate yearly tax 
     #countdown of values in the dictionary until the last value 
     totals = list(pDict.values())[count] 
     totals = int(totals) 
     names = list(pDict.keys())[count] 
     #decleration of variable to store tax 
     tTax = 0 
     if totals < 1000: 
      tTax = totals * 0 
     elif totals > 1000 and totals<= 10000: 
      tTax = 1000 * 0 
      totals = totals - 1000 
      tTax = tTax + totals * 0.1 
     elif totals > 10000 and totals <=20200: 
      tTax = 1000 * 0 
      tTax = tTax + 9000 * 0.1 
      totals=totals-10000 
      tTax = tTax + totals * 0.15 
     elif totals >20200 and totals <= 30750: 
      tTax = 1000 * 0 
      tTax = tTax + 9000 * 0.1 
      tTax = tTax + 10200 * 0.15 
      totals=totals-20200 
      tTax = tTax + totals * 0.2 
     elif totals>30750 and totals<=50000: 
      tTax = 1000 * 0 
      tTax = tTax + 9000 * 0.1 
      tTax = tTax + 10200 * 0.15 
      tTax = tTax + 10550 * 0.2 
      totals=totals-30750 
      tTax = tTax + totals * 0.25 
     else: 
      tTax = 1000 * 0 
      tTax = tTax + 9000 * 0.1 
      tTax = tTax + 10200 * 0.15 
      tTax = tTax + 10550 * 0.2 
      tTax = tTax + 19250 * 0.25 
      totals=totals-50000 
      tTax = tTax + totals * 0.3 
     dict_list.setdefault(names,tTax) 
     count = count + 1 
     return dict_list 
    except(attributeError,TypeError): 
     raise ValueError('The provided input is not a dictionary') 
    else: 
    print("only dict type values allowed") 

私のコードが動作するかどうかをテストするために使用されるコード:以下のように

from unittest import TestCase 

    class CalculateTaxTests(TestCase): 
     def test_it_calculates_tax_for_one_person(self): 
     result = calculate_tax({"James": 20500}) 
     self.assertEqual(result, {"James": 2490.0}, msg="Should return {'James': 2490.0} for the input {'James': 20500}") 

     def test_it_calculates_tax_for_several_people(self): 
     income_input = {"James": 20500, "Mary": 500, "Evan": 70000} 
     result = calculate_tax(income_input) 
     self.assertEqual({"James": 2490.0, "Mary": 0, "Evan": 15352.5}, result, 
      msg="Should return {} for the input {}".format(
       {"James": 2490.0, "Mary": 0, "Evan": 15352.5}, 
       {"James": 20500, "Mary": 500, "Evan": 70000} 
     ) 
     ) 

     def test_it_does_not_accept_integers(self): 
     with self.assertRaises(ValueError) as context: 
      calculate_tax(1) 
      self.assertEqual(
      "The provided input is not a dictionary.", 
      context.exception.message, "Invalid input of type int not allowed" 
     ) 

     def test_calculated_tax_is_a_float(self): 
     result = calculate_tax({"Jane": 20500}) 
     self.assertIsInstance(
      calculate_tax({"Jane": 20500}), dict, msg="Should return a result of data type dict") 
     self.assertIsInstance(result["Jane"], float, msg="Tax returned should be an float.") 

     def test_it_returns_zero_tax_for_income_less_than_1000(self): 
     result = calculate_tax({"Jake": 100}) 
     self.assertEqual(result, {"Jake": 0}, msg="Should return zero tax for incomes less than 1000") 

     def test_it_throws_an_error_if_any_of_the_inputs_is_non_numeric(self): 
     with self.assertRaises(ValueError, msg='Allow only numeric input'): 
      calculate_tax({"James": 2490.0, "Kiura": '200', "Kinuthia": 15352.5}) 

     def test_it_return_an_empty_dict_for_an_empty_dict_input(self): 
     result = calculate_tax({}) 
     self.assertEqual(result, {}, msg='Should return an empty dict if the input was an empty dict') 

参考のため

答えて

6

:-)助けてください、失敗するテストからの完全な例外メッセージは、次のとおりです。

ERROR: test_it_does_not_accept_integers (__main__.CalculateTaxTests) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "calculate_tax_test.py", line 27, in test_it_does_not_accept_integers 
    context.exception.message, "Invalid input of type int not allowed" 
AttributeError: '_AssertRaisesContext' object has no attribute 'exception' 

失敗したテストは次のとおりです:

 def test_it_does_not_accept_integers(self): 
     with self.assertRaises(ValueError) as context: 
      calculate_tax(1) 
      self.assertEqual(
      "The provided input is not a dictionary.", 
      context.exception.message, "Invalid input of type int not allowed" 
     ) 

問題は、calculate_taxの後のアサーションが間違った場所にあることです。例外がcalculate_taxの場合、アサーションはスキップされます。例外が発生しない場合、アサーションは失敗します。したがってアサーションは決して通過しません。

修正点は、アサーションをインデントしてwithステートメントから移動させることです。一つはcalculate_taxへの呼び出しによって発生してしまった場合

 def test_it_does_not_accept_integers(self): 
     with self.assertRaises(ValueError) as context: 
      calculate_tax(1) 

     self.assertEqual(
      "The provided input is not a dictionary.", 
      context.exception.message, "Invalid input of type int not allowed" 
     ) 

with self.assertRaises(...)文が例外をキャッチすることができます明確にするために私はまた、空白行を挿入しました。この場合、例外の詳細はcontextに残され、アサーションは例外が期待どおりかどうかをテストできます。

しかし、calculate_tax(1)ValueErrorを生成しないため、この変更を行った後もテストは失敗します。私はこれを修正するためにそれをあなたに任せます。

+0

ありがとう! –

関連する問題