如何在Python中通过unittest类执行代码后修复多个错误消息



我创建了一段代码,它在三个不同的函数上迭代一个字典。

主要功能如下:


> def main():
> # initial roster
>     brave_roster = {      "Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273",       "Ronald Acuna": "AB: 467, R: 71, H: 124, HR: 15,
> AVG: 0.266",   } ```
> 
> The Three Functions are as follows: ``` def
> lookup_player(brave_roster, name):   name = input("Enter the name of
> the player you want to lookup:n")
>      if name in brave_roster:
>     
>     print("Here are the", [name], "stats:", brave_roster[name])   else:
>     print("That player does not exist.")
> 
> def add_player_to_dict(brave_roster, name, stats):   name =
> input("Enter the name of the player you want to add:n")
>      if name not in brave_roster:
>     stats = input("Please add Matt's stats:n")
>     brave_roster[name] = stats
>     brave_roster.update([(name, stats)])
>     print("Here's", name,"'s stats:n")
>     for name, stats in brave_roster.items():
>      print(name, ':', stats)   else:
>     print("That player is already on our roster.")
> 
> def delete_in_dict(brave_roster, name):   name = input("Enter the name
> of the player you want to remove:n")   if name in brave_roster:
>     del brave_roster[name]   else:
>     print("n uh typo?", name, "does not play for us")
> 
> if __name__ == '__main__':   print("t ***  Braves Stats!  ***n")  
> print("Welcome to My Braves Stats!")   brave_roster = {
>     "Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273",
>     "Ronald Acuna": "AB: 467, R: 71, H: 124, HR: 15, AVG: 0.266",   }
>      choice = input("Please type your choice number:") if (choice == "1"):
>     lookup_player(brave_roster) elif (choice == "2"):
>       add_player_to_dict(brave_roster) else: 
>     (choice == "3")
>     delete_in_dict(brave_roster)        print("nThanks for using my Braves Stats.")
> 
> ```
> 
> Essentially from these three functions and the main code I'm supposed
> to use inputs to look up, update, or delete a roaster of players from
> a baseball team.
> 
> I have to make sure the earlier code runs in the test codes. However,
> I keep getting errors such as:
> 
> > **FAIL: test_add_player_duplicate (__main__.TestDictFunctions.test_add_player_duplicate)
> > ---------------------------------------------------------------------- Traceback (most recent call last): line 106, in
> > test_add_player_duplicate
> >     self.assertEqual(actual, expected) AssertionError: {'Austin Riley': 'AB: 615, R: 90, H: 168,[80 chars]214'} != None**

What am I doing wrong?

Here is the test file:

import unittest类TestDictFunctions(unittest. testcase):

def testrongearch_player_success(自我):test_dict = {"Austin Riley";AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273"}actual = test_dict["Austin Riley"]expected = lookup_player(test_dict, "Austin Riley")自我。实际assertEqual(预期)

def testrongearch_player_no_result(自我):test_dict = {"Austin Riley";AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273"}actual = "N/A"expected = lookup_player(test_dict, "Ronald Acuna")自我。实际assertEqual(预期)

def test_add_player_sucess(自我):test_dict = {"Austin Riley";AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273"}actual = {"Austin Riley";AB: 615, R: 90, H: 168, HR: 38, AVG: 0.272 ";Ronald Acuna";AB: 467, R: 71, H: 124, HR: 15, AVG: 0.266"}expected = add_player_to_dict(test_dict, "Ronald Acuna", "AB: 467, R: 7 ' ' 1, H: 124, HR: 15, AVG: 0.266")

self.assertEqual(actual, expected)

def test_add_player_duplicate(自我):test_dict = {"Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273"}actual = {"Austin Riley";AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273"; "Austin Riley(2)"; "AB: 350, R: 20, H: 120, HR: 5, AVG: 0.214"}expected = add_player_to_dict(test_dict, "Austin Riley", "AB: 350, R: 20, H: 120, HR: 5, AVG: 0.214")自我。实际assertEqual(预期)

def test_delete_player_sucess(自我):test_dict = {"Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273"}期望= {}actual = delete_in_dict(test_dict, "Austin Riley")自我。实际assertEqual(预期)

def test_delete_word_no_result(自我):test_dict = {"Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273"}Expected = test_dictactual = delete_in_dict(test_dict, "Shohei Ohtani")自我。实际assertEqual(预期)

unittest.main()
<代码>

您得到的错误是因为测试代码将三个参数传递给add_player_to_dict函数,但函数定义只接受一个参数(brave_roster)。

要修复此错误,您应该修改函数定义以接受额外的参数player_name和player_stats,如下所示:

expected = {"Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273", "Ronald Acuna": "AB: 467, R: 71, H: 124, HR: 15, AVG: 0.266", "Austin Riley(2)": "AB: 350, R: 20, H: 120, HR: 5, AVG: 0.214"}
然后,在测试代码中,您可以修改期望的变量,以包含新的球员名称和统计:

expected = add_player_to_dict(test_dict, "Austin Riley(2)", "AB: 350, R: 20, H: 120, HR: 5, AVG: 0.214")

调用函数时,传递brave_roster, player_name和player_stats参数:

PP_6通过这些更改,测试代码应该可以无错误地运行。

还要注意,在delete_in_dict函数中,你应该在删除球员后添加一个return brave_roster语句,这样修改后的字典就会返回给调用代码。

最新更新