python:如何为这个函数编写测试?

python:如何为这个函数编写测试?,python,testing,pytest,typeerror,Python,Testing,Pytest,Typeerror,我有我的功能和测试部分 测试部分, 但是我得到了这种错误,我如何修复它,我需要使用类型转换吗 TypeError: unsupported operand type(s) for -: 'str' and 'str' 例如,我的文件中有aa bb cc。这意味着我有3个字段,如果我想得到真值,我需要传递字段\u number=3看看你传递给decorator的第一个参数-'file\u name,fields\u number=None,sep='-',预期的\u result' 由于'-'的

我有我的功能和测试部分

测试部分, 但是我得到了这种错误,我如何修复它,我需要使用类型转换吗

TypeError: unsupported operand type(s) for -: 'str' and 'str'

例如,我的文件中有aa bb cc。这意味着我有3个字段,如果我想得到真值,我需要传递字段\u number=3

看看你传递给decorator的第一个参数-
'file\u name,fields\u number=None,sep='-',预期的\u result'

由于
'-'
的原因,它看起来像是在减去两个字符串:
'file\u name,fields\u number=None,sep='
',预期的结果'
。不确定是否需要包含默认值,但如果确实使用双引号,则不要在包含单引号的字符串周围使用单引号

编辑:这应该可以:

@pytest.mark.parametrize('file_name, fields_number, sep, expected_result',
                         [('file_prog', 5, '-', True),
                          ('file_prog', None ,'-', False)])
def test_func(file_name, fields_name, sep, expected_result):
    result = func(file_name=file_name, fields_number=fields_number, sep=sep)
    assert result == expected_result
可以用列表替换装饰器的第一个参数:


['file\u name','fields\u number','sep','expected\u result']
查看传递给decorator的第一个参数-
'file\u name,fields\u number=None,sep='-',expected\u result'

由于
'-'
的原因,它看起来像是在减去两个字符串:
'file\u name,fields\u number=None,sep='
',预期的结果'
。不确定是否需要包含默认值,但如果确实使用双引号,则不要在包含单引号的字符串周围使用单引号

编辑:这应该可以:

@pytest.mark.parametrize('file_name, fields_number, sep, expected_result',
                         [('file_prog', 5, '-', True),
                          ('file_prog', None ,'-', False)])
def test_func(file_name, fields_name, sep, expected_result):
    result = func(file_name=file_name, fields_number=fields_number, sep=sep)
    assert result == expected_result
可以用列表替换装饰器的第一个参数:


['file\u name'、'fields\u number'、'sep'、'expected\u result']

发布您得到的完整回溯。我怀疑这和你代码的那部分有关。另外请注意,
func
需要
字段数
关键字参数,并且在测试中通过了
num\u fileds
。对不起,这是我的错,我忘了在这里更改它。但我的问题在于参数化部分,因为当我调用这个函数并给出参数时,这个函数就起作用了@buranplease,不要删除问题的重要内容,因为这会使问题变得毫无用处。发布你得到的完整回溯。我怀疑这和你代码的那部分有关。另外请注意,
func
需要
字段数
关键字参数,并且在测试中通过了
num\u fileds
。对不起,这是我的错,我忘了在这里更改它。但我的问题在于参数化部分,因为当我调用这个函数并给出参数时,这个函数就起作用了@请不要删除问题中的重要内容,让它变得毫无用处。你是说这样的吗[('file_prog',5,“-”,True),('file_prog',None,“-”,False)]“@buranThanks,我现在就试试看)@buranYou是这样的意思吗?”[('file_prog',5,“-”,True),('file_prog',None,“-”,False)]“@buranThanks,我现在就试试看)@buran