You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于关键字使用tox运行pytest测试并解决导入错误?

Using pytest -k with tox: Fixing Import Errors & Proper Workflow

Absolutely, using tox -- -k <keyword> is the correct way to run targeted tests with tox—your import errors are just a configuration issue, not a flaw in the syntax itself. Let’s break down why this happens and how to fix it, so you can stop waiting for full test runs.

Why You’re Seeing Import Errors

Tox creates isolated, "clean" environments by default, which means it won’t automatically install your local project or its dependencies unless you explicitly tell it to. When you run tox -- -k <keyword> without proper configuration, pytest in the tox environment can’t find your local modules, hence the import failures.

Fixing Your tox.ini Configuration

The fix is straightforward: update your tox.ini to ensure your local project and dependencies are installed in each test environment. Here’s a standard working configuration:

[tox]
envlist = py39, py310, py311  # Adjust to your target Python versions
skipsdist = true  # Skip building a source distribution if using develop mode

[testenv]
# Install pytest and your project's dependencies
deps =
    pytest
    -r requirements.txt  # Include this if you have a requirements file
# Install your local project in editable mode (like `pip install -e .`)
usedevelop = true
# Pass all positional arguments to pytest (key for forwarding `-k`)
commands = pytest {posargs}

Key Details:

  • usedevelop = true: This tells tox to install your local project in editable mode, so code changes are reflected in the test environment without re-installing.
  • commands = pytest {posargs}: The {posargs} placeholder passes all arguments after -- directly to pytest—this is what lets you use -k, -x, --tb=short, etc., with tox.

Running Targeted Tests Now

With the updated config, you can run keyword-based tests exactly how you tried before:

# Run all tests matching "login" across all tox environments
tox -- -k "login"

# Run tests matching "payment" only in Python 3.10
tox -e py310 -- -k "payment"

# Combine with other pytest flags, like stopping on first failure
tox -- -k "checkout" -x

Bonus: Shortcuts for Frequent Use

If you find yourself typing this often, add a custom testenv to your tox.ini for quicker access:

[testenv:kw]
# Reuse base testenv settings
basepython = python3.10  # Leave out to run across all envs
deps = {[testenv]deps}
usedevelop = {[testenv]usedevelop}
commands = pytest -k {posargs}

Then run it with:

tox -e kw "user_registration"

Final Notes

  • Double-check that your tox.ini includes all necessary dependencies (via deps or requirements.txt)—missing dependencies will also cause import errors.
  • If you don’t want editable mode, replace usedevelop = true with deps = . (to install your project from the current directory) instead.

This setup lets you leverage tox’s isolated environments while running targeted tests with pytest’s -k flag, saving you those 2-minute full test runs.

内容的提问来源于stack exchange,提问作者Leonidaz0r

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:11:47