简体   繁体   English

pytest 只运行更改后的文件?

[英]pytest run only the changed file?

I'm fairly new to Python, trying to learn the toolsets.我对 Python 相当陌生,正在尝试学习工具集。

I've figured out how to get py.test -f to watch my tests as I code.我已经想出了如何让py.test -f在我编码时观看我的测试。 One thing I haven't been able to figure out is if there's a way to do a smarter watcher, that works like Ruby's Guard library.我无法弄清楚的一件事是,是否有办法做一个更智能的观察者,就像 Ruby 的 Guard 库一样。

Using guard + minitest the behavior I get is if I save a file like my_class.rb then my_class_test.rb is executed, and if I hit enter in the cli it runs all tests.使用 guard + minitest 我得到的行为是,如果我保存一个像my_class.rb这样的文件,那么my_class_test.rb被执行,如果我在 cli 中按enter ,它会运行所有测试。

With pytest so far I haven't been able to figure out a way to only run the test file corresponding to the last touched file, thus avoiding the wait for the entire test suite to run until I've got the current file passing.使用 pytest 到目前为止,我还没有找到一种方法来只运行与上次接触的文件相对应的测试文件,从而避免等待整个测试套件运行,直到我通过当前文件。

How would you pythonistas go about that?你pythonistas会怎么做?

Thanks!谢谢!

One possibility is using pytest-testmon together with pytest-watch .一种可能性是将pytest-testmonpytest-watch一起使用。

It uses coverage.py to track which test touches which lines of code, and as soon as you change a line of code, it re-runs all tests which execute that line in some way.它使用coverage.py来跟踪哪个测试涉及哪些代码行,一旦您更改了一行代码,它就会重新运行以某种方式执行该行的所有测试。

To add to @The Compiler 's answer above, you can get pytest-testmon and pytest-watch to play together by using pytest-watch's --runner option:要添加到上面@The Compiler的答案,您可以使用 pytest-watch 的--runner选项让pytest-testmonpytest-watch一起玩:

ptw --runner "pytest --testmon"

Or simply:或者干脆:

ptw -- --testmon

There is also pytest-xdist which has a feature called:还有pytest-xdist有一个功能叫做:

--looponfail: run your tests repeatedly in a subprocess. --looponfail:在子进程中重复运行您的测试。 After each run py.test waits until a file in your project changes and then re-runs the previously failing tests.每次运行 py.test 后,会等待项目中的文件发生更改,然后重新运行之前失败的测试。 This is repeated until all tests pass after which again a full run is performed.重复此操作,直到所有测试通过,然后再次执行完整运行。

If you are using git as version control, you could consider using pytest-picked .如果您使用git作为版本控制,则可以考虑使用pytest-picked This is a plugin that according to the docs:这是一个插件,根据文档:

Run the tests related to the unstaged files or the current branch运行与未暂存文件或当前分支相关的测试

Demo演示

在此处输入图片说明

Basic features基本功能

  • Run only tests from modified test files仅从修改后的测试文件运行测试
  • Run tests from modified test files first, followed by all unmodified tests首先从修改后的测试文件运行测试,然后是所有未修改的测试

Usage用法

pytest --picked

The fastest setup I got was when I combines @lmiguelvargasf @BenR and @TheCompiler answer into this我得到的最快的设置是当我将 @lmiguelvargasf @BenR 和 @TheCompiler 的答案结合到这个

ptw --runner "pytest --picked --testmon"

you first gotta have them installed by你首先要安装它们

pip3 install pytest-picked pytest-testmon pytest-watch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM