Python unittest - 在 0.000 秒内运行 0 个测试 [英] Python unittest - Ran 0 tests in 0.000s

查看:35
本文介绍了Python unittest - 在 0.000 秒内运行 0 个测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I want to do this code Kata for practice. I want to implement the kata with tdd in separate files:

算法:

# 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()

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

When running the testfile, I get:

Ran 0 tests in 0.000s

OK

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

It should return one failed test however. What do I miss here?

推荐答案

如 python unittest 中所述 文档:

As stated in the python unittest doc:

最简单的 TestCase 子类将简单地实现一个测试方法(即名称以 test 开头的方法)

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

因此,您需要将方法名称更改为如下所示:

So you will need to change your method name to something like this:

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

这篇关于Python unittest - 在 0.000 秒内运行 0 个测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆