[英]Unit testing in PyCharm
好的,所以我正在为单元测试章节做 Python 速成课程练习,但我无法使用 PyCharm 让它工作。
我的“项目”字面上由两个简单的文件组成:
city_functions.py:
def city_country(city, country):
result = '{0}, {1}'.format(city.title(), country.title())
return result
test_cities.py:
import unittest
from city_functions import city_country
class CityTestCase(unittest.TestCase):
"""Tests for 'city_functions.city_country' function."""
def test_city_country(self):
result = city_country('london', 'england')
self.assertEqual(result, 'London, England')
unittest.main()
现在,当我尝试从 PyCharm 运行模块时,我得到:
(...)
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
但它有效,当我从命令行运行它时:
> python -m test_cities
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
是否有任何带有 PyCharm 配置的恶作剧来让它工作? 我读过一些关于命名约定的帖子(测试函数和整个模块必须以“test”开头,但在我的例子中已经这样做了)。
实际上添加主要检查解决了这个问题(它从 PyCharm 和命令行工作):
(...)
if __name__ == '__main__':
unittest.main()
我在 PyCharm 上遇到了完全相同的问题。 我正在学习 Python 速成课程,并按照第 11 章中的说明逐步进行操作。我还必须使用:
if__name__ == '__main__':
unittest.main()
让测试工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.