【问题标题】:Python unittest returns "Ran 0 tests in 0.000s" and I have no idea whyPython unittest 返回“在 0.000 秒内运行 0 次测试”,我不知道为什么
【发布时间】:2018-06-09 13:54:30
【问题描述】:

我被这个问题困住了,我不知道为什么这个测试不起作用。请任何人帮助我。

下面的代码在 cityname.py 文件中

def get_name(city, country):
    return (city.title() + ", " + country.title())

下面的代码在 test_cities.py 文件中

import unittest

from cityname import get_name

class CitiesTestCase(unittest.TestCase):

    def test_city_country(self):
        santiago_chile = get_name('santiago', 'chile')
        self.assertEqual(santiago_chile, 'Santiago, Chile')

unittest.main()

这是输出

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

OK

【问题讨论】:

  • 如果您发布的代码中的缩进与文件中的内容匹配,那是因为test_city_country函数不属于CitiesTestCase,因为没有缩进
  • 为我工作。你是如何运行它的?
  • 我知道它应该可以工作,但我认为在导入我的文件时可能存在某种问题(?),我不知道如何处理它。我在pycharm中正常运行它,我没有办法运行它。

标签: python unit-testing testing


【解决方案1】:
  1. 要么删除unittest.main()
  2. 或者传递参数exit=Falseunittest.main(exit=False)
  3. 或者用这个if 声明包围unittest.main()

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

您可以在 Python 文档中阅读有关其工作原理的更多信息... https://docs.python.org/3.6/library/unittest.html#unittest.main

【讨论】:

    【解决方案2】:

    就我而言,我同时安装了python2和python3,“python”指的是py2。 “python -m unittest -v”总是返回“Ran 0 test”,由“python3 -m unittest -v”修复

    【讨论】:

      猜你喜欢
      • 2017-10-12
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多