Python 3.x Python中的单元测试
我对单元测试完全陌生,不明白为什么我会得到“断言错误:出乎意料地没有” 我的笔试是Python 3.x Python中的单元测试,python-3.x,Python 3.x,我对单元测试完全陌生,不明白为什么我会得到“断言错误:出乎意料地没有” 我的笔试是 import unittest from Extraction import Core class testDict(unittest.TestCase): def test_dict_populate(self): result=Core(file='input.txt') self.assertIsNotNone(result) 从提取文件是我的核心功能 def
import unittest
from Extraction import Core
class testDict(unittest.TestCase):
def test_dict_populate(self):
result=Core(file='input.txt')
self.assertIsNotNone(result)
从提取文件是我的核心功能
def Core(file):
with open(file) as f:
for line in f:
# populate auction item
if 'SELL' in line:
auctionsplit = (line.strip().split('|'))
new_item = auction(auctionsplit)
dct[new_item] = []
elif 'BID' in line:
tokens = (line.strip().split('|'))
new_bid = bid(tokens)
bid_item_id = new_bid.item
key_to_update = find_item(bid_item_id, dct.keys())
dct[key_to_update].append(new_bid)
else:
pass
输入文件与测试和核心存储在同一个项目中,因此您的函数核心不会返回值。在python中,不隐式返回值的函数将返回None。在测试用例中,
result
被分配到None,测试用例失败
您可能希望Core返回某个值您的函数Core不返回值。在python中,不隐式返回值的函数将返回None。在测试用例中,
result
被分配到None,测试用例失败
您可能希望Core返回一些内容
Core
不返回任何内容。您的意思是要以某种方式检查dct?我不认为有任何方法可以在函数之外访问它。它需要返回或成为某个类或模块的一部分(我认为它实际上是,因为它在任何地方都没有初始化为dict)。Core
不返回任何内容。您的意思是要以某种方式检查dct?我不认为有任何方法可以在函数之外访问它。它需要返回或成为某个类或模块的一部分(我认为它实际上是这样的,因为它在任何地方都没有初始化为dict)。