首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python unitest不起作用

Python unitest不起作用
EN

Stack Overflow用户
提问于 2017-10-09 08:52:42
回答 1查看 137关注 0票数 0

我开始学TDD了。我刚刚从python的单元测试开始。当我试图执行:

代码语言:javascript
复制
vagrant@vagrant:~/pruebaTestPython$ python test_python_daily_software.py 

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

我在其他links中读到,我需要在开始时用test_重命名自己的函数。然而,这正是我所做的,但仍然不起作用。

test_python_daily_software.py文件:

代码语言:javascript
复制
#!/usr/bin/python
# -*- coding: utf-8 -*-

import unittest
import python_daily

class TestPythonSoftware(unittest.TestCase):

    def test_should_return_python_when_number_is_3(self):
        self.assertEqual('Python', python_daily.get_string(3))

    def test_should_return_daily_when_number_is_5(self):
        self.assertEqual('Daily', python_daily.get_string(5))

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

和python_daily.py文件:

代码语言:javascript
复制
#!/usr/bin/python
# -*- coding: utf-8 -*-

def get_string(number):
    return 'Hello'

怎么啦?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-09 09:42:07

如果您的python_daily.py Python模块是:

代码语言:javascript
复制
#!/usr/bin/python
# -*- coding: utf-8 -*-


def get_string(number):
    return 'Hello'

您的test_python_daily_software.py测试模块是:

代码语言:javascript
复制
#!/usr/bin/python
# -*- coding: utf-8 -*-

import unittest

import python_daily


class TestPythonSoftware(unittest.TestCase):
    def test_should_return_python_when_number_is_3(self):
        self.assertEqual('Python', python_daily.get_string(3))

    def test_should_return_daily_when_number_is_5(self):
        self.assertEqual('Daily', python_daily.get_string(5))


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

你应该:

代码语言:javascript
复制
$ python test_python_daily_software.py 
FF
======================================================================
FAIL: test_should_return_daily_when_number_is_5 (__main__.TestPythonSoftware)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_python_daily_software.py", line 11, in test_should_return_daily_when_number_is_5
    self.assertEqual('Daily', python_daily.get_string(5))
AssertionError: 'Daily' != 'Hello'

======================================================================
FAIL: test_should_return_python_when_number_is_3 (__main__.TestPythonSoftware)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_python_daily_software.py", line 8, in test_should_return_python_when_number_is_3
    self.assertEqual('Python', python_daily.get_string(3))
AssertionError: 'Python' != 'Hello'

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (failures=2)

请注意源代码中的缩进!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46642360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档