Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 Unittest_Python_Python 3.x_Unit Testing_Python Unittest - Fatal编程技术网

名称错误:名称';方法名称';未定义Python Unittest

名称错误:名称';方法名称';未定义Python Unittest,python,python-3.x,unit-testing,python-unittest,Python,Python 3.x,Unit Testing,Python Unittest,我有以下项目结构: 每个文件的代码如下所示(init.py中不包含任何内容): my_sum.py 计算\u sum.py: def calculate(arg): total = 0 for val in arg: total += val return total def create_sum(): print("hello") total = calculate([5, 5]) print(total) import u

我有以下项目结构:

每个文件的代码如下所示(init.py中不包含任何内容):

my_sum.py

计算\u sum.py

def calculate(arg):
    total = 0
    for val in arg:
        total += val
    return total

def create_sum():
    print("hello")
    total = calculate([5, 5])
    print(total)
import unittest

from dir import *

class TestSum(unittest.TestCase):
    def test_list_int(self):
        """
        Test that it can sum a list of integers
        """
        data = [1, 2, 3]
        result = sum(data)
        self.assertEqual(result, 6)

if __name__ == '__main__':
    unittest.main()
测试总和py:

如您所见,这是一个非常简单的程序,用于计算列表的总和注意:我在模拟一个更大的项目,因此我有多个(相当不必要的)方法调用

当我从
MainDir
目录运行
python-m unittest discover-s test/
时,我得到一个错误:

testsum.py”,第11行,在test\u list\u int中 结果=计算(数据)名称错误:未定义名称“计算”

但是,如果我将“计算”方法更改为“求和”(即下面的方法),我的测试运行良好。注意:我应该注意,我是从一个网站在线复制了这个示例,因此,我认为“求和”是在某个地方定义的,但我不确定在哪里,因此不知道错误发生的原因

计算_sum.py更改为以下值将通过测试:

def sum(arg):
    total = 0
    for val in arg:
        total += val
    return total

def create_sum():
    print("hello")
    total = sum([5, 5])
    print(total)
testsum.py

def calculate(arg):
    total = 0
    for val in arg:
        total += val
    return total

def create_sum():
    print("hello")
    total = calculate([5, 5])
    print(total)
import unittest

from dir import *

class TestSum(unittest.TestCase):
    def test_list_int(self):
        """
        Test that it can sum a list of integers
        """
        data = [1, 2, 3]
        result = sum(data)
        self.assertEqual(result, 6)

if __name__ == '__main__':
    unittest.main()
----------------------------------------------------------------------在0.000秒内运行了1次测试

sum(\u iterable)
是python中的内置方法。因此它没有调用您的方法,而是调用了内置方法

在测试方法中,您没有像在主模块中那样导入
服务
模块


从MainDir.dir.service.calculate\u sum import calculate将
添加到您的测试模块中,一切都会正常工作。

如果删除“创建\u sum”会发生什么“从最初的例子中,没有经历过?就我所知,它什么也没做。。。至少就单位而言-test@monamona在本例中,它不起任何作用。但正如我提到的,我在模仿一个更大的项目,这就是为什么我有额外的方法调用。无论如何,如果我删除“create_sum”并直接从main方法调用“sum”,我会得到相同的错误。谢谢。但是我得到了一个错误“ModuleNotFoundError:没有名为'service'的模块”,它必须是我在回答中更改的dir.service再次感谢,但我得到了另一个错误:“ImportError:无法从'dir.service'(未知位置)导入名称'calculate'”您的方法名称仍然是calculate或sum?它是calculate。我没有在任何方法中包含“总和”