【问题标题】:How to run only specific directory using tox in pytest?如何在pytest中使用tox仅运行特定目录?
【发布时间】:2021-07-13 12:45:43
【问题描述】:

test_pytest.py 位于测试目录中,它包含用于测试机器学习工作流程的自动化代码(以下方法)。

 def test_workflow(caplog,test_name,test_params,test_step):,
 def test_upload_and_import_data(input_params, output_params,):,
 def test_feature_eng(input_params, output_params):,
 def test_auto_training(input_params, output_params, problem_type):,
 def test_get_prediction(input_params, output_params):
 def test_retraining(response):
 def delete_ai_service():

QA_test 目录位于 test_data->tests 中。 它包含 json 文件,为不同数据集的上述方法提供输入和预期输出。 例如,

{

"Car_prices dataset" : [
    {
        "dataImport": {
            "input": {
                "test_filename": "carprices.csv",
                "file_location": "AWS"
            },
            "expectedOutput": {
                "result" : "success"
            }
        }
    },
    {
        "featureEngineering": {
            "input": {
            "column": "MSRP",
            "waitTime": 60,
            "problemType" : "auto"
    
            },
            "expectedOutput": {
    "result" : "Completed"
            }
        }
    },
    '
    '
    '

]

}

这是现有的 tox 文件。

[tox]
   envlist = py38

[testenv]
deps =
   pytest
   pytest-html
   pytest-sugar
   pytest-logger
   allure-pytest
   pytest-xdist
   pytest_steps
   datetime
   oauth2client
   gspread
   aiclub
commands =
   pytest -s -v -k _workflow  --html=test_report.html --alluredir=allure-results/ -n auto --dist=loadfile
   allure serve allure-results

目前,当我在终端中键入 tox 时,它会运行整个测试套件,运行所有数据集的所有工作流。 如何仅针对 QA_test 目录中的测试用例(json 文件)运行测试工作流程?

【问题讨论】:

    标签: python json pytest tox


    【解决方案1】:

    您需要使用toxposargs 功能 - 这让您可以随时指定特定命令。

    您使用pytest 作为测试运行者。 -k 选项指定要运行的测试模式。

    您不需要将其硬编码为_workflow,而是需要执行类似的操作

    commands = 
         pytest -s -v {posargs:-k _workflow} ...
    

    然后您可以通过

    运行单个测试
    tox -- -k singleTest
    

    如果您只是运行tox,则使用默认值 (-k _workflow)。

    双破折号后面的所有内容都被传递到命令中 - 在本例中为 pytest

    tox 功能 posargs 记录在 https://tox.readthedocs.io/en/latest/example/general.html

    对于pytest,请查看https://docs.pytest.org/en/6.2.x/usage.html#specifying-tests-selecting-tests

    【讨论】:

    • 错误:命令 'D:\QA\test-framework\python-client\.tox\py38\Scripts\pytest.EXE' -s -v -k QA_tests --html=test_report 的 InvocationError .html --alluredir=allure-results/ -n auto --dist=loadfile(退出代码5)
    • @J.G.当我使用“pytest -s -v {posargs:-k _workflow} --html=test_report.html --alluredir=allure-results/ -n auto --dist=loadfile”并运行“tox -- -k QA_tests”时在终端给出了上述错误。
    • @ShelomiPriskila 不幸的是,我看不到明显的问题。当我遇到类似的问题时,我尝试使命令尽可能简单,确保它有效,然后将其他选项一一添加回来。也许从commands = commands = pytest {posargs:-k _workflow}开始...
    • @J.G.对于“pytest {posargs:-k _workflow}”命令也给出相同的调用错误
    猜你喜欢
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 2019-01-13
    • 2016-08-29
    • 2019-04-17
    相关资源
    最近更新 更多