4

platform linux -- Python 3.6.7, pytest-4.4.0, py-1.8.0, pluggy-0.9.0

#example.py
try:
    import configparser
except ImportError:
    import ConfigParser as configparser

CONFIG = configparser.ConfigParser()
CONFIG.read(sys.argv[1])
ININFO = {i:dict(CONFIG.items(i)) for i in CONFIG.sections()}
DATANAME = ININFO['data']['name']

def somefunction(DATANAME):
    """
        This function will take lot of variables from ini file
    """
    print(DATANAME)
    s1 = "Pass"
    s2 = "Fail"
    s3 = "Pass"
    print(s1, s2, s3)
    return [s1, s2, s3]

def test_somefunction():
    """
        Test function
           - Will check whether all steps are passed or not.
    """
    status = somefunction()
    for sts in status:
        assert sts == 'Pass', "Test has Failed!"

if __name__ == "__main__":

    somefunction()

Same like above code I have so many files and all have built-in tests

How I execute is

#sudo python3 example.py inifile.ini

Please let me know How I can execute this with pytest and with out changing the code.

if i replace sys.argv[1] with filename this work fine as below

sudo python3 -m pytest -s 

Guide me how I can handle this.

I added and check below screenshot error image

example ini file test.ini

[data]
name = some.name
age = 22

[data1]
name = someother.name
age = 32
sreedhar
  • 43
  • 1
  • 4

1 Answers1

1

As I commented before you just have to create a python file to call pytest and arguments, from there pytest will be triggered.

$ python run.py example.py test.ini

Here, run.py is as below

$ cat run.py
# run.py
import pytest
import sys

def main():
    # extract your arg here
    print('Extracted arg is ==> %s' % sys.argv[2])
    pytest.main([sys.argv[1]])

if __name__ == '__main__':
    main()

example.py is your python script.

test.ini is your ini file.

Hoping this clarified your query.

All credits goes to this answer

kalehmann
  • 4,821
  • 6
  • 26
  • 36
raj trans
  • 26
  • 3