DEV Community

Dinesh
Dinesh

Posted on

Pytest How to pass .yaml file as argument in pytest command line

I have multiple .yaml files each containing shell or python commands and I want to execute them separately based on the need through command line arguments in pytest execution .

test_code.yaml

**.yaml file content :**

test_execute_impala_query:
- change_dir_path: /home/a/
  sql_result: ksh -x /home/impala_query.sh /sandbox/home/impala_export_query.sql  /sandbox/home/actual_result.csv output_delimiter=,

test_file_compare:
 - file_compare_check: python3 /sandbox/pytest_execution/test_compare_file.py  --file1 /sandbox/venv/smoke_test/actual_result_prsn.csv --file2 /sandbox/smoke_test/expected_result.csv 

Enter fullscreen mode Exit fullscreen mode

Actual Pytest script

test_code_check.py

import pytest
import os
import parametrize_from_file
import pandas as pd
import subprocess
import sys


@parametrize_from_file
def test_execute_impala_query(change_dir_path,sql_result):
    os.chdir(change_dir_path)
    result = subprocess.run([sql_result],shell=True,check=True,stdout=subprocess.PIPE, universal_newlines=True)
    assert result.returncode == 0



@parametrize_from_file
def test_file_compare(file_compare_check):
    result = subprocess.run([file_compare_check],shell=True,check=True,stdout=subprocess.PIPE, universal_newlines=True)
    assert result.returncode == 0
Enter fullscreen mode Exit fullscreen mode

Here, the above code perfectly works fine but that .yaml filename I need to pass it through command line

I have to execute the below way to get the output separately.

pytest test_code_check.py --test_file test_code.yaml  

pytest test_code_check.py --test_file test_code_2.yaml 

pytest test_code_check.py --test_file test_code_3.yaml  

pytest test_code_check.py --test_file test_code_4.yaml 
Enter fullscreen mode Exit fullscreen mode

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs