簡體   English   中英

使用 Pytest 和 Tox 測試 Test 目錄中的所有文件

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

我可以使用 Tox 成功執行 pytest(只需在 Pycharm 的終端中輸入“tox”)。 但是,當我從 Pycharm 在終端中鍵入pytest -s tests時,沒有執行測試。 ImportError: DLL load failed: The specified module could not be found.

即使您可以在下面的tox.ini中看到commands = pytest -s tests並且在 Tox 中測試成功,那么為什么我無法在終端中執行pytest -s tests

目錄結構

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]
    envlist = py37

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

設置.cfg

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

安裝程序.py

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

tox 的默認工作目錄是toxinit ,您可以嘗試將其更改為tests

參考這里:https://tox.readthedocs.io/en/latest/config.html#conf-changedir

我認為您需要運行python setup.py develop 因為當您運行 TOX 時,它會將您的項目安裝在 tox virtualenv 中。 你需要為你的本地 virtualenv 做同樣的事情(如果你使用它,我推薦)。

我建議您使用 pycharm(PyCharm->File->Settings->Project->Python Interpreter->Add)創建本地 virtualenv,然后當您在 PyCharm 中運行終端時,您將在此 virtualenv 中。 python setup.py develop 有了這個,您可以使用 PyCharm 在本地運行測試(右鍵單擊測試並運行)。 但首先在設置中選擇 pytest 作為 PyCharm 中的測試框架。

如果你想集成 Tox 和 PyCharm 一起工作並在 PyCharm 中運行 tox 測試,我在這里寫了一篇關於它的博文。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM