【发布时间】:2017-07-25 14:05:07
【问题描述】:
好的,所以我正在为单元测试章节做 Python Crash Course 练习,但我无法使用 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”开头,但在我的情况下已经这样做了)。
【问题讨论】:
-
是的,在尝试运行单元测试时肯定需要进行一些设置。很难给出一步一步的指示。仔细阅读help 并查看配置菜单以了解您可以使用哪些选项。查看在哪里您正在运行
python -m test_cities命令以了解用于使其工作的相对路径。 -
最终,在您的配置中,您需要确定“工作”目录是应用程序的主要执行位置,而这又是您导入的参考。跨度>
标签: python unit-testing pycharm