VSCode中Pytest Fixture报错:缺少必填位置参数declare_hexidict
问题原因及解决办法
核心问题
你的测试函数命名不符合Pytest的规则——Pytest默认只会把以test_开头的函数识别为测试用例。你写的setitem_getitem没有这个前缀,导致Pytest没有按照测试函数的逻辑处理它,也就不会自动注入declare_hexidict这个fixture参数,最终抛出参数缺失的错误。
修复步骤
直接修改测试函数的名称,给它加上test_前缀:
def test_setitem_getitem(declare_hexidict): print('start') # hd = hexidict() # rvc = ReferenceValueCluster() # rv = ReferenceValue(init=3) hd, rvc, rv = declare_hexidict print('datastruct defined') hd[rvc("key1").reflink] = rv[0].reflink hd[rvc["key1"]] == rv[0] assert rvc["key1"] in hd.keys(), "key :{} is not int this hexidict".format( rvc("key1") ) assert hd[rvc["key1"]] == rv[0], "key :{} return {} instead of {}".format( rvc["key1"], hd[rvc["key1"]], rv[0] ) #set non value item (on set une liste) hd[rvc("key2").reflink] = [rv[1].reflink] hd[rvc["key2"]] assert type(hd[rvc["key2"]]) == list #on verifie que l'item dans la list est bien celui qui provient de rv assert hd[rvc["key2"]][0] in rv
验证
修改后重新运行Pytest,它会正确识别这个测试函数,并自动注入declare_hexidict fixture,你的测试逻辑就能正常执行了。
内容的提问来源于stack exchange,提问作者Chrys Bltr
相关产品推荐
相关产品推荐

