Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 代码在单元测试时运行_Python_Python 3.x_Unit Testing - Fatal编程技术网

Python 代码在单元测试时运行

Python 代码在单元测试时运行,python,python-3.x,unit-testing,Python,Python 3.x,Unit Testing,我正在尝试对我用python制作的扫雷游戏进行单元测试。我从一个定义上的一个测试开始,但是它运行了整个代码,我应该测试其中一个非常小的部分。 单元测试代码: import unittest from minesweeper import setupgrid class Testmyfunctions(unittest.TestCase): def test_setup(self): self.assertTrue(setupgrid(9, [], 10)) if _

我正在尝试对我用python制作的扫雷游戏进行单元测试。我从一个定义上的一个测试开始,但是它运行了整个代码,我应该测试其中一个非常小的部分。 单元测试代码:

import unittest
from minesweeper import setupgrid

class Testmyfunctions(unittest.TestCase):
    def test_setup(self):
        self.assertTrue(setupgrid(9, [], 10))


if __name__ == '__main__' :
    unittest.main(exit=False)
这是它应该检查的功能:

import random, re, time
from string import ascii_lowercase


def setupgrid(gridsize, start, numberofmines):
    emptygrid = [['0' for i in range(gridsize)] for i in range(gridsize)]
    mines = getmines(emptygrid, start, numberofmines)
    for i, j in mines:
        emptygrid[i][j] = 'X'
    grid = getnumbers(emptygrid)
    return (grid, mines)

emptygrid和numberofmines的默认值分别为9和10,start的值应为空,因此[]。

您的缩进被破坏,无论是在实际代码中还是在此处发布的代码中。@chepner对不起,您能解释一下“缩进”的含义吗?我是一个编程高手,不是一个母语为英语的人。你在这里发布的代码没有正确的缩进;例如,
setupgrid
的正文中没有一行缩进,而
for
循环的正文正确缩进。缩进是文本前一行的空白。Python使用行的缩进来确定代码块中的内容。例如,在
def setupgrid(gridsize,start,numberofmines):
行之后,属于该函数的所有代码行都应位于该行的右侧,以表明它们是
setupgrid
函数的主体。我怀疑这只是复制粘贴代码的一种情况。您能描述/显示在运行unittest时意外运行的代码吗?另外,如果您根据
setupgrid()
函数的预期返回值进行断言,则在运行时和有人查看您的测试时,您的
test\u设置
方法会提供更多信息。实际上,任何“truthy”的返回值,即
bool(value)
返回
True
,都会导致测试通过。例如,即使您的安装方法返回
(None,None)
,这也将通过您的测试,即使我假设此返回值不是预期值。您的缩进被破坏,无论是在实际代码中还是在此处发布的代码中。@chepner对不起,您能解释一下“缩进”的含义吗?我是一个编程高手,不是一个母语为英语的人。你在这里发布的代码没有正确的缩进;例如,
setupgrid
的正文中没有一行缩进,而
for
循环的正文正确缩进。缩进是文本前一行的空白。Python使用行的缩进来确定代码块中的内容。例如,在
def setupgrid(gridsize,start,numberofmines):
行之后,属于该函数的所有代码行都应位于该行的右侧,以表明它们是
setupgrid
函数的主体。我怀疑这只是复制粘贴代码的一种情况。您能描述/显示在运行unittest时意外运行的代码吗?另外,如果您根据
setupgrid()
函数的预期返回值进行断言,则在运行时和有人查看您的测试时,您的
test\u设置
方法会提供更多信息。实际上,任何“truthy”的返回值,即
bool(value)
返回
True
,都会导致测试通过。例如,即使您的安装方法返回
(None,None)
,这也将通过测试,即使我假设此返回值不是预期值。