简体   繁体   English

Pytest:如何从测试方法外部访问命令行参数

[英]Pytest: How to access command line arguments from outside a test method

I have a NON-TEST method defined in a python module.我在 python 模块中定义了一个非测试方法。 (Say- read_test_data()), which has to consume the test_ID's passed through the command line. (比如 read_test_data()),它必须使用通过命令行传递的 test_ID。 I have another test-method, defined in a class, which calls read_test_data() and gets the values from there.我有另一个在类中定义的测试方法,它调用 read_test_data() 并从那里获取值。 Now, how can I get the values from command line arguments, into the non-test method?现在,如何将命令行参数中的值获取到非测试方法中?

if your function is located on a file called file_name.py, that would be something like this:如果您的函数位于一个名为 file_name.py 的文件中,那将是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM