问题描述
我尝试关注 youtube pytest 研讨会。 我使用 VS Code 来处理它。并学会正确使用 VS Code...
所以我有包含函数 total 和 join 的文件 ls24_module.py。 我在测试文件中导入的,据我所知它有效并且是正确的。甚至 pytest 也显示所有 6 个测试都通过了。
但是 VS Code 仍然显示 test_join_use_case 的错误:Fail: NameError: name 'join' is not defined.
并且错误在第 17 行。这是第一次正确。我在导入中添加了 join 并将所有内容都写下来。但错误并没有消失。所以我复制了这部分测试并将其包含到最后。因为测试的顺序并不重要。但这仍然表明问题消息不会自行更新。由于def test_join_use_case()
现在定义在第 23 行。但我没有找到删除问题的方法,让 VS Code 再次检查文件。它只表明第17行仍然存在问题。
VSCode 中未更新的问题消息图片:
使用的文件是:
ls24_module.py
from typing import List
# skeleton function
def total(xs: List[float]) -> float:
"""Total return the sum of xs."""
result: float = 0.0
"""For each x float in xs,add it to result"""
for x in xs:
result += x
return result
def join(xs: List[int],delimiter: str) -> str:
"""Produce a string where subsequent items are separated by delimiters."""
generated_string: str = ""
for item in xs:
if generated_string == "": # Don't put delimiter before first
generated_string = str(item)
else:
generated_string += delimiter + str(item)
return generated_string
ls24_module_test.py
"""An example of a test module in pytest."""
from ls24_module import total,join
def test_total_empty() -> None:
"""Total of empty list should be 0.0"""
assert total([]) == 0.0
def test_total_single_item() -> None:
"""Total of a single item list should be the first item's value."""
assert total([110]) == 110.0
def test_total_multiple_item() -> None:
"""Total of a multiple item list should be the sum of the items in the list."""
assert total([1.0,2.0,3.0]) == 6.0
def test_join_edge_single_item() -> None:
assert join([1],",") == "1"
def test_join_edge_empty_delimiter() -> None:
assert join([1,2,3],"") == "123"
def test_join_use_case() -> None:
assert join([1,") == "1,3"
有人能帮忙吗。
更新
已解决:重新启动 VS Code 并重新加载 Windows 后,错误消失。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)