我有一段时间搞清楚这一点,这真的让我烦恼,所以我想我会发布这个以防万一有人遇到同样的问题......
(答案是如此简单,它会伤害:-)
问题
问题的核心是,有时候,并非总是如此,当在PyTest中处理返回对象的灯具时,当你在PyCharm的测试中使用这些灯具时,你不会得到自动完成的提示.如果您在编写测试时想要引用具有大量方法的对象,则会给测试编写过程带来很多开销和不便.
这是一个简单的例子来说明这个问题:
假设我有一个"event_manager"类,它位于:
location.game.events
Run Code Online (Sandbox Code Playgroud)
让我们进一步说,在我的conftest.py文件中(对于不熟悉的PyTest标准事物),我有一个返回该类实例的fixture:
from location.game.events import event_manager
...
@pytest.fixture(scope="module")
def event_mgr():
"""Creates a new instance of event generate for use in tests"""
return event_manager()
Run Code Online (Sandbox Code Playgroud)
我有时会遇到问题(但并不总是 - 我不能完全弄明白为什么)这样的类,其中自动完成在我使用灯具的测试代码中无法正常工作,例如
def test_tc10657(self, evt_mgr):
"""Generates a Regmod and expects filemod to be searchable on server"""
evt_mgr.(This does not offer autocomplete hints when you type ".")
Run Code Online (Sandbox Code Playgroud)
所以答案实际上很简单,一旦你在PyCharm中查看类型提示:http: //www.jetbrains.com/help/pycharm/2016.1/type-hinting-in-pycharm.html
以下是修复上述测试代码以便自动完成正常工作的方法:
from location.game.events import event_manager
...
def test_tc10657(self, evt_mgr: event_manager):
"""Generates a Regmod and expects filemod to be searchable on …
Run Code Online (Sandbox Code Playgroud) 我正在运行 Python 3.7.4,并且在处理某些事情时发现了一些不良行为,然后我将其简化为:
>>> x = 5
>>> x -= 1 if False else print("blah")
blah
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -=: 'int' and 'NoneType'
Run Code Online (Sandbox Code Playgroud)
除非有什么明显的东西我只是想念?为什么它甚至试图评估 -= 如果它落入其他?