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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay