I have a NON-TEST method defined in a python module. (Say- read_test_data()), which has to consume the test_ID's passed through the command line. I have another test-method, defined in a class, which calls read_test_data() and gets the values from there. Now, how can I get the values from command line arguments, into the non-test method?
Asked
Active
Viewed 572 times
-3
-
not clear what you are asking and there's no [mcve] – Devstr Oct 03 '18 at 22:42
1 Answers
1
if your function is located on a file called file_name.py, that would be something like this:
import argparse
def read_test_data(testcase_id):
print(testcase_id)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--testcase_id", help="test case id helper documentation")
args = parser.parse_args()
read_test_data(args.testcase_id)
if you make a call from command line like:
python file_name.py --testcase_id "test1A, test1B"
You will get as output:
test1A, test1B

Rodrigo López
- 4,039
- 1
- 19
- 26
-
This definitely works. But the position of the argument seems to be hardcoded. And if I enter a few extra options while running the code, it'll fail. For instance if I give my command as pytest -s -r file_name.py --testcase_id="test1A, test1B", it wouldnt work. Is there a way to get the argument values based on the argument name? As in, I want to check if "--testcase_id" is in the argument list. If yes, I want to get "test1A, test1B" – Namratha Oct 03 '18 at 22:58
-
also, I was looking at using pytest libraries and fixtures. the ArgumentParser python library doesnt seem to work for my case – Namratha Oct 03 '18 at 23:17
-
this helps you? https://stackoverflow.com/questions/40880259/how-to-pass-arguments-in-pytest-by-command-line – Rodrigo López Oct 03 '18 at 23:21