简体   繁体   English

使用 Pytest 和 Tox 测试 Test 目录中的所有文件

[英]Test all files in Test directory with Pytest and Tox

I could execute pytest with Tox(Just type "tox" in terminal from Pycharm) successfully.我可以使用 Tox 成功执行 pytest(只需在 Pycharm 的终端中输入“tox”)。 But when I typed pytest -s tests in Terminal from Pycharm, Then Test was not executed.但是,当我从 Pycharm 在终端中键入pytest -s tests时,没有执行测试。 ImportError: DLL load failed: The specified module could not be found.

Even though you can see commands = pytest -s tests in tox.ini below and test was succeed in Tox, Then why I couldn't execute pytest -s tests in Terminal??即使您可以在下面的tox.ini中看到commands = pytest -s tests并且在 Tox 中测试成功,那么为什么我无法在终端中执行pytest -s tests

Directory Structure目录结构

root
| - .tox
| - robotor # include all of source code in "robotor" directory
| - tests
| | | - unittest
| | | | - robotor
| | | | | - __init__.py
| | | | | - test_robotor.py
| | | | - __init.py
| | | - __init__.py
| - main.py
| - setup.py
| - setup.cfg
| - tox.ini

tox.ini毒物

    [tox]
    envlist = py37

    [testenv:py37]
    deps = pytest
    commands = pytest -s tests  # I put test files in "tests" directory

setup.cfg设置.cfg

[aliases]
test=pytest
[tool:pytest]
python_files=tests/*

setup.py安装程序.py

setup(
    ~
  ~
    setup_requires=[
        'pytest-runner',
    ],
    tests_require=['pytest'],
  ~
    ~
)

The default working dir for tox is toxinit , can you try changedir to tests . tox 的默认工作目录是toxinit ,您可以尝试将其更改为tests

Reference here:https://tox.readthedocs.io/en/latest/config.html#conf-changedir参考这里:https://tox.readthedocs.io/en/latest/config.html#conf-changedir

I think you need to run python setup.py develop .我认为您需要运行python setup.py develop Cause when you run TOX it's installing your project inside tox virtualenv.因为当您运行 TOX 时,它会将您的项目安装在 tox virtualenv 中。 You need to do same thing for your local virtualenv (if you use it, I recommend).你需要为你的本地 virtualenv 做同样的事情(如果你使用它,我推荐)。

I would recommend you to create local virtualenv with pycharm (PyCharm->File->Settings->Project->Python Interpreter->Add) and then when you run Terminal inside PyCharm you will be inside this virtualenv.我建议您使用 pycharm(PyCharm->File->Settings->Project->Python Interpreter->Add)创建本地 virtualenv,然后当您在 PyCharm 中运行终端时,您将在此 virtualenv 中。 The python setup.py develop . python setup.py develop With this you can run tests localy with PyCharm (right click on tests and just run).有了这个,您可以使用 PyCharm 在本地运行测试(右键单击测试并运行)。 But first choose in settings pytest as tests freamwork in PyCharm.但首先在设置中选择 pytest 作为 PyCharm 中的测试框架。

If you want to integrate Tox and PyCharm to work togheter and run tox tests inside PyCharm I wrote a blogpost about it here .如果你想集成 Tox 和 PyCharm 一起工作并在 PyCharm 中运行 tox 测试,我在这里写了一篇关于它的博文。

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

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