【问题标题】:vscode-python runs pytest in wrong directory such that relative paths get confusedvscode-python 在错误的目录中运行 pytest,导致相对路径混淆
【发布时间】:2019-04-25 22:14:46
【问题描述】:

使用 pytest 和 vscode-python,我想运行之前使用 unittest 框架实现的测试。

因此,我在相应目录 tests 中使用 pytest 成功运行了测试。

pytest

我还设置了 vscode-python 并成功测试了几乎所有的测试。

但是,那些从子目录 tests/data 加载数据的测试失败了,因为 vscode-python 似乎从另一个目录而不是测试目录 tests 运行 pytest。

abc/
|-- tests/
    |-- test_function.py
    |-- data/

如何设置 vscode-python,以便成功读取已经实现的测试中的所有数据文件?

【问题讨论】:

    标签: python-3.x vscode-python


    【解决方案1】:

    导航到“设置”(在 macOS 上的 VS Code 1.35.0 中):

    菜单“代码”>>“首选项”>>“设置”

    在“设置”窗格中,搜索“python.testing.cwd”。

    文档说 'cwd' '为单元测试指定一个可选的工作目录。'

    对于您的设置,为“python.testing.cwd”生成相对路径的一种方法是:

    1. 在 VS Code 编辑器中打开“abc/tests/data/”下的数据文件之一
    2. 在编辑器中右键单击打开文件的选项卡,然后从下拉菜单中选择“复制相对路径”
    3. 将该相对路径粘贴到“python.testing.cwd”配置字段中(并在保存前对其进行适当编辑)

    或者,您可能想研究使用一个或多个VS Code Variables 指定路径。

    参考文献:

    1. [https://code.visualstudio.com/docs/python/unit-testing#_test-configuration-settings]
    2. [https://code.visualstudio.com/docs/editor/variables-reference]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      • 2013-08-06
      • 2020-02-15
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      • 2012-10-11
      相关资源
      最近更新 更多