如何在pytest中使用tox仅运行特定目录?

问题描述

This is the structure of the test framework

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,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 文件)运行测试工作流?

解决方法

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

您使用 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