【问题标题】:bash script doesn't read pytest filesbash 脚本不读取 pytest 文件
【发布时间】:2022-05-30 04:24:21
【问题描述】:

我正在为我的 django 项目使用 pytest 编写几个测试用例。我在根目录中创建了一个.ini 文件,如下所示:

python_files = 
    tests.py
    test_*.py
    *_tests.py
    *_test.py

norecursedirs =
    devops
    docs
    media
    settings
    common
    templates
    
addopts = 
    --maxfail=9999
    --showlocals
    --color=yes
    --runxfail
    --strict-markers
    --durations=30
    -r a
    ; --reuse-db
    --no-migrations
    --pdbcls=IPython.terminal.debugger:TerminalPdb

并在主目录中创建了一个文件夹 /devops 和一个名为 backend_tests.sh 的 bash 文件,如:

#!/usr/bin/env bash

options+=(\"--cov\")
options+=(\"--disable-warnings\")
echo \"launch pytest ${options[@]}\"

poetry run pytest \"${options[@]}\"

if [[ $? == 1 ]]; then exit 1; fi

当我运行devops/backend_tests.sh 时,它显示,已收集 0 件商品但是当我将backend_tests.sh 移动到主目录并运行./backend_tests.sh 它显示收集了 5 个项目(这意味着加载测试用例。

  • 这是意料之中的——pytest 使用 Python 路径。您可以从脚本的根路径运行pytest,也可以将根路径添加到 Python 路径。
  • 啊,谢谢它现在可以工作了。如果你写作为答案我会接受

标签: bash directory pytest pytest-django


【解决方案1】:

如果没有给pytest 提供参数,它会从当前路径递归discovers tests。所以如果你想使用相同的命令,你可以改变它的执行路径:

pushd <test_path>
poetry run pytest
popd

您还可以提供测试或测试路径on the command line,因此最短的方法就是:

poetry run pytest <test_path>

(在您的情况下,&lt;test_path&gt; 可能只是..

【讨论】:

    猜你喜欢
    • 2014-10-26
    • 1970-01-01
    • 2014-10-28
    • 2010-12-30
    • 2019-08-03
    • 2020-06-18
    • 2021-05-16
    • 2016-07-17
    • 1970-01-01
    相关资源
    最近更新 更多