在vscode中运行pytest测试的单个文件夹

问题描述 投票:0回答:2

我有一种类似于以下内容的“微服务” Python存储库:

sample
├── bar
│   ├── src
│   │   └── main.py
│   └── tests
│       └── test_main.py
├── foo
│   ├── src
│   │   └── main.py
│   └── tests
│       └── test_main.py
└── shared
    ├── src
    │   └── main.py
    └── tests
        └── test_main.py

在vscode中,我只能选择在foobarshared中运行all测试,或在子文件夹中运行单独的测试methods。我想做的就是能够快速运行foo/tests/

是否可以通过某种方式配置pytest / Python来执行此操作?我不想将每个顶级文件夹拆分到自己的工作区中,因为我经常跳回去并在它们之间跳转,并且不想每个工作区都打开多个窗口。

python pytest vscode-settings vscode-python
2个回答
0
投票

您应该能够根据以下说明在终端中运行命令pytest foo/tests/pytest documentation


0
投票

主要有两种选择,

  1. 通过在命令行本身上指定位置来运行特定位置测试,您可以通过按Ctrl + ~来获取命令行
    • 例如pytest foo/tests/
  2. 另一种方法是在项目根目录下创建pytest配置文件。配置文件应命名为pytest.ini

    • 并将testpaths变量分配给所需的文件夹

一个示例如下

[pytest]
testpaths = foo
python_files = tests.py test_*.py *_tests.py *.tests.py

这只会在foo目录下运行测试

© www.soinside.com 2019 - 2024. All rights reserved.