相关疑难解决方法(0)

sys.argv [1]在脚本中的含义

我现在正在教自己Python,只是想简单地说(参考下面的例子)sys.argv [1]代表什么.它只是要求输入吗?

#!/usr/bin/python3.1

# import modules used here -- sys is a very standard one
import sys

# Gather our code in a main() function
def main():
  print ('Hello there', sys.argv[1])
  # Command line args are in sys.argv[1], sys.argv[2] ..
  # sys.argv[0] is the script name itself and can be ignored

# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':
  main()
Run Code Online (Sandbox Code Playgroud)

python

135
推荐指数
6
解决办法
37万
查看次数

如何将命令行参数从 pytest 传递给代码

我正在尝试将参数从 pytest 测试用例传递给正在测试的模块。例如,使用Python 样板中的 main.py ,我可以从命令行运行它:

$ python3 main.py
usage: main.py [-h] [-f] [-n NAME] [-v] [--version] arg
main.py: error: the following arguments are required: arg
$ python3 main.py xx
hello world
Namespace(arg='xx', flag=False, name=None, verbose=0)
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试对 pytest 做同样的事情,使用以下 test_sample.py

注意: main.py 需要命令行参数。但是这些参数需要在特定测试中进行硬编码,它们不应该是 pytest 的命令行参数。pytest 测试用例只需要将这些值作为命令行参数发送给 main。主要的()。)

import main
def test_case01():
    main.main()
    # I dont know how to pass 'xx' to main.py,
    # so for now I just have one test with no arguments
Run Code Online (Sandbox Code Playgroud)

并按以下方式运行测试:

pytest -vs …
Run Code Online (Sandbox Code Playgroud)

pytest argparse python-3.x

9
推荐指数
2
解决办法
3949
查看次数

Pytest - 测试解析器错误:无法识别的参数

我正在尝试测试一个非常简单的函数(由于多次尝试测试使用参数解析器作为参数的更复杂的函数失败)。

# 来自 ./runfile.py

import argparse
import os


def get_input_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--priv_raw_name', default='private_data.csv', type=str,
                    help='Set raw private/source datafile name')
    args = parser.parse_args()
    return args
Run Code Online (Sandbox Code Playgroud)

# 来自 ./tests/test_args.py

import pytest
from runfile import get_input_args

def test_parser():
    parser = get_input_args()
    assert 1
Run Code Online (Sandbox Code Playgroud)

当我运行脚本时,runfile.py一切都按预期工作,但是当我打电话时,pytest tests/test_args.py我收到以下无法识别的参数错误消息。

我试过设置一个conftest.py文件并使用 addoption,但我似乎遇到了远远超出我修复经验的更多错误。谁能指出我如何使用核心脚本中的默认参数运行测试的正确方向,但没有tests/test_args.py被称为实际测试本身的参数?

    (Project_Cascade) ?  Project_Cascade git:(WIP) ? pytest tests/test_args.py
========================================================================= test session starts =========================================================================
platform darwin -- Python 3.6.5, pytest-4.3.1, py-1.8.0, pluggy-0.9.0
rootdir: /Users/davidmellor/Code/Spend_Network/Data_Projects/Project_Cascade, inifile:
collected 1 item …
Run Code Online (Sandbox Code Playgroud)

python testing pytest

5
推荐指数
1
解决办法
4213
查看次数

如何在pytest中处理sys.argv?

平台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: …
Run Code Online (Sandbox Code Playgroud)

python pytest python-3.x

5
推荐指数
1
解决办法
7671
查看次数

标签 统计

pytest ×3

python ×3

python-3.x ×2

argparse ×1

testing ×1