gpt4 book ai didi

Python unittest - 在 0.000 秒内运行 0 次测试

转载 作者:IT老高 更新时间:2023-10-28 20:44:25 25 4
gpt4 key购买 nike

所以我想做这个代码 Kata练习。我想在单独的文件中使用 tdd 实现 kata:

算法:

# stringcalculator.py  
def Add(string):
return 1

和测试:

# stringcalculator.spec.py 
from stringcalculator import Add
import unittest

class TestStringCalculator(unittest.TestCase):
def add_returns_zero_for_emptyString(self):
self.assertEqual(Add(' '), 0)

if __name__ == '__main__':
unittest.main()

运行测试文件时,我得到:

Ran 0 tests in 0.000s

OK

但是它应该返回一个失败的测试。我在这里想念什么?

最佳答案

如python unittest doc 中所述:

The simplest TestCase subclass will simply implement a test method (i.e. a method whose name starts with test)

因此,您需要将方法名称更改为以下内容:

def test_add_returns_zero_for_emptyString(self):
self.assertEqual(Add(' '), 0)

关于Python unittest - 在 0.000 秒内运行 0 次测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43957860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com