好的,所以我正在做单元测试章节的Python Crash Course exercies,我无法完全使用PyCharm来使用它。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](https://www.jetbrains.com/help/pycharm/run-debug-configuration-unittests.html),然后查看配置菜单以查看可用的选项。看看你在哪里运行你的'python -m test_cities'命令来理解正在使用的相对路径。 – idjaw
最终,在您的配置中,您需要确定“工作”目录是哪个应用程序的主执行位置,哪个是您的导入位置的参考。 – idjaw