首页 > 解决方案 > Pytest BDD:尝试使用场景装饰器时“TypeError:'NoneType'对象不可调用”

问题描述

我有许多具有相同功能文件格式的测试,但传递不同的参数并断言不同的 json 响应,所以我想scenariosscenario. 我尝试使用的装饰器是:

@scenarios("../../features/errors.feature")

当我尝试使用时,scenarios我收到以下错误,我无法真正找出问题所在:

==================================== ERRORS ====================================
_______ ERROR collecting functional/tests/test_api/test_api_error.py ________
src/test/functional/tests/test_api/test_api_error.py:17: in <module>
    def get_data(v4_auth, get_url, from_date, to_date):
E   TypeError: 'NoneType' object is not callable
=========================== short test summary info ============================
ERROR src/test/functional/tests/test_api/test_api_error.py - TypeError: 'N...
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.11s ===============================

如果我使用scenario装饰器并为功能文件中的每个场景提供一个带有 pass 的存根方法,那么这可以正常工作,但现在我有很多本质上冗余的函数来声明场景。

例如:

@scenario(
    "../../features/error.feature",
    "Retrieve data with an invalid from date format",
)
def invalid_from_date():
    pass

@scenario(
    "../../features/errors.feature",
    "Retrieve data with an invalid to date format",
)
def invalid_to_date():
    pass

@scenario(
    "../../features/errors.feature",
    "Retrieve data with all invalid params",
)
def invalid_params():
    pass

使用scenario装饰器时,所有装置都可以正常工作并且测试全部通过,我认为从阅读文档中我需要做的就是使用scenarios装饰器来代替它。

它所在的行始终是它尝试调用的第一个函数的声明。

标签: pytestpytest-bdd

解决方案


推荐阅读