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

Python 在vscode中运行pytest测试的单个文件夹,python,pytest,vscode-settings,vscode-python,Python,Pytest,Vscode Settings,Vscode Python,我有一种微服务Python repo,其设置类似于以下内容: sample ├── bar │ ├── src │ │ └── main.py │ └── tests │ └── test_main.py ├── foo │ ├── src │ │ └── main.py │ └── tests │ └── test_main.py └── shared ├── src │ └── main.py └── tes

我有一种微服务Python repo,其设置类似于以下内容:

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中,我只能选择在foo、bar、shared中运行所有测试,或者在子文件夹中运行单个测试方法。我想做的是能够快速运行foo/tests/


有什么方法可以配置pytest/Python来实现这一点吗?我不想将每个顶级文件夹拆分为自己的工作区,因为我经常在它们之间跳回和for,并且不希望每个工作区打开多个窗口。

您应该能够根据

您应该能够根据
您主要有两种选择

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

并将testpaths变量分配到所需的文件夹 下面是一个示例

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


这将只运行foo目录下的测试

主要有两个选项供您选择

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

并将testpaths变量分配到所需的文件夹 下面是一个示例

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


这将只运行foo目录下的测试

对,但我希望能够通过vscode中的命令运行。对,但我希望能够通过vscode中的命令运行。您可以在[pytest]部分下创建addopts=foo/tests/的pytest.ini。您可以在[pytest]下创建addopts=foo/tests/的pytest.ini这影响了测试发现。他必须修改pytest.ini,重新发现测试,每次他想在不同的文件夹中运行测试时都要运行测试,这会影响测试发现。他必须修改pytest.ini,重新发现测试,每次他想在不同的文件夹中运行测试时都要运行测试