python单元测试练习

来源:互联网 发布:sql case when else 编辑:程序博客网 时间:2024/06/01 09:11

IDE:Pycharm Professional

生命游戏单元测试:

生命游戏是英国数学家约翰·何顿·康威在1970年发明的细胞自动机,它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死亡的细胞。一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死了的细胞的数量。如果相邻方格活着的细胞数量过多,这个细胞会因为资源匮乏而在下一个时刻死去;相反,如果周围活细胞过少,这个细胞会因太孤单而死去。

游戏在一个类似于围棋棋盘一样的,可以无限延伸的二维方格网中进行(在程序实现中,我们采取令左右边界相接、上下边界相接的方法模拟无限棋盘的情况)。例如,设想每个方格中都可放置一个生命细胞,生命细胞只有两种状态:“生”或“死”。图中,用黑色的方格表示该细胞为“死”, 其它颜色表示该细胞为“生” 。游戏开始时, 每个细胞可以随机地(或给定地)被设定为“生”或“死”之一的某个状态, 然后,再根据如下生存定律计算下一代每个细胞的状态:

  1. 每个细胞的状态由该细胞及周围8个细胞上一次的状态所决定;
  2. 如果一个细胞周围有3个细胞为生,则该细胞为生,即该细胞若原先为死则转为生,若原先为生则保持不变;
  3. 如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变;
  4. 在其它情况下,该细胞为死,即该细胞若原先为生则转为死,若原先为死则保持不变。

这里提供四个python文件(下载),分别是main.py、life_game.py、game_timer.py、game_map.py,它可以正确地实现生命游戏的功能(棋盘的左右、上下是相连的),通过python main.py即可启动。本次作业要求针对game_map.py中的接口编写测试用例,测试文件需要命名为test_game_map.py。

说明:在测试正确的前提下,本次作业将根据代码覆盖率进行评分。


使用pycharm进行单元测试的步骤

  • 打开要测试的文件
  • 菜单Navigate–>test
    这里写图片描述
  • 点击创建一个Test
    这里写图片描述
  • 勾选要测试的方法,确定
    这里写图片描述
  • 编写测试代码
from itertools import chainfrom itertools import cyclefrom unittest import TestCasefrom unittest import mockfrom game_map import GameMapclass TestGameMap(TestCase):    def setUp(self):        self.game_map = GameMap(4,3)    def test_init(self):        self.assertRaises(TypeError, GameMap, ('a', 3))        self.assertRaises(TypeError, GameMap, (4, 'b'))    def test_rows(self):        self.assertEqual(4,self.game_map.rows,"Should get correct rows")    def test_cols(self):        self.assertEqual(3, self.game_map.cols, "Should get correct rows")    @mock.patch('random.random',new=mock.Mock(side_effect=chain(cycle([0.3, 0.6, 0.9]))))    def test_reset(self):        self.game_map.reset()        for i in range(0, 4):            self.assertEqual(1, self.game_map.get(i, 0))            for j in range(1, 3):                self.assertEqual(0, self.game_map.get(i, j))        self.assertRaises(TypeError, self.game_map.reset, possibility='ab')    def test_get_set(self):        self.assertEqual(0, self.game_map.get(0,0),"Cells init to 0")        self.game_map.set(0, 0, 1)        self.assertEqual(1, self.game_map.get(0, 0), "Should get value set by set")        self.assertRaises(TypeError, self.game_map.get, ("d3d3f", 0))        self.assertRaises(TypeError, self.game_map.get, (0, 'b'))        self.assertRaises(TypeError, self.game_map.set, ('a', 0, 1))        self.assertRaises(TypeError, self.game_map.set, (0, 'b', 1))        self.assertRaises(TypeError, self.game_map.set, (0, 0, 'c'))    def test_get_neighbor_count(self):        expected_value = [[8]*3]*4        self.game_map.cells = [[1]*3]*4        for i in range(0,4):            for j in range(0,3):                self.assertEqual(expected_value[i][j],self.game_map.get_neighbor_count(i, j), '(%d %d)' % (i, j))        self.assertRaises(TypeError, self.game_map.get_neighbor_count, ('a', 0))        self.assertRaises(TypeError, self.game_map.get_neighbor_count, (0, 'b'))    @mock.patch('game_map.GameMap.get_neighbor_count', new=mock.Mock(return_value=8))    # game_map.GameMap.get_neighbor_count    def test_get_neighbor_count_map(self):        expected_value = [[8] * 3] * 4        self.assertEqual(expected_value, self.game_map.get_neighbor_count_map())    def test_set_map(self):        self.assertRaises(TypeError,self.game_map.set_map,{(0,0):1})        self.assertRaises(AssertionError,self.game_map.set_map,[[1]*3]*3)        self.assertRaises(TypeError,self.game_map.set_map,[['1']*3]*4)        self.assertRaises(AssertionError,self.game_map.set_map,[[2]*3]*4)        self.game_map.set_map([[1]*3]*4)        self.assertEqual([[1]*3]*4,self.game_map.cells)    def test_print_map(self):        self.game_map.cells = [            [0, 1, 1],            [0, 0, 1],            [1, 1, 1],            [0, 0, 0]        ]        with mock.patch('builtins.print') as mock1:            self.game_map.print_map()            mock1.assert_has_calls([                mock.call('0 1 1'),                mock.call('0 0 1'),                mock.call('1 1 1'),                mock.call('0 0 0'),            ])
  • 点击按钮运行测试
    这里写图片描述
  • 测试结果
    这里写图片描述

注意

  • assertRaises()方法能正确断言异常,但不会增加测试覆盖率

参考: mooc资料

0 0
原创粉丝点击