python单元测试练习
来源:互联网 发布:sql case when else 编辑:程序博客网 时间:2024/06/01 09:11
IDE:Pycharm Professional
生命游戏单元测试:
生命游戏是英国数学家约翰·何顿·康威在1970年发明的细胞自动机,它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死亡的细胞。一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死了的细胞的数量。如果相邻方格活着的细胞数量过多,这个细胞会因为资源匮乏而在下一个时刻死去;相反,如果周围活细胞过少,这个细胞会因太孤单而死去。
游戏在一个类似于围棋棋盘一样的,可以无限延伸的二维方格网中进行(在程序实现中,我们采取令左右边界相接、上下边界相接的方法模拟无限棋盘的情况)。例如,设想每个方格中都可放置一个生命细胞,生命细胞只有两种状态:“生”或“死”。图中,用黑色的方格表示该细胞为“死”, 其它颜色表示该细胞为“生” 。游戏开始时, 每个细胞可以随机地(或给定地)被设定为“生”或“死”之一的某个状态, 然后,再根据如下生存定律计算下一代每个细胞的状态:
- 每个细胞的状态由该细胞及周围8个细胞上一次的状态所决定;
- 如果一个细胞周围有3个细胞为生,则该细胞为生,即该细胞若原先为死则转为生,若原先为生则保持不变;
- 如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变;
- 在其它情况下,该细胞为死,即该细胞若原先为生则转为死,若原先为死则保持不变。
这里提供四个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
- python单元测试练习
- Python单元测试练习
- Python练习代码 -- 单元测试 使用unittest
- python单元测试
- Python 单元测试
- python单元测试
- python 单元测试
- Python单元测试
- Python单元测试
- python 单元测试
- python单元测试
- Python 单元测试
- python单元测试
- python单元测试
- python单元测试
- python单元测试
- python 单元测试
- python-单元测试
- 安卓项目中常用的第三方框架推荐
- Create_first_WDA
- cookie应用显示当前与之前时间
- 浅析“远程对象调用”
- 地图整饰-框架与格网
- python单元测试练习
- PHP 对象-数组转换
- 【Window内核驱动开发】——通过符号链接获取真实设备
- NOIP2016全国信息学分区联赛提高组第二试 组合问题
- Spring事务传播机制和数据库隔离级别
- 开发技巧笔记
- PowerDesigner 反向生成
- Entity Framework 6 Code First系列3: 实体类M:N配置
- Linux下编译APM工具要求