python - 如何为pytest传递参数

标签 python unit-testing testing pydev pytest

我正在尝试测试我的类(class)中指定的方法。我一直在通过全局执行此操作以传递到我的方法中,例如,

csv.preprocess_col(df, field, remove_invalid_rows_or_raise_exception =0)

使用全局 df 到路径。

但现在我想通过函数传递参数通过执行 test_df(df) 而不是常规 test_df() 来测试我的函数 ( csv.preprocess_col) 在里面,但我一直收到这个 fixture df not found 错误。

谁知道怎么解决?

只是一个好奇的问题,对于 pytest 来说,可以通过全局将参数传递给函数吗?

最佳答案

pytest 在 several well-integrated ways 中支持测试参数化:

  • pytest.fixture() allows to define parametrization at the level of fixture functions.

  • @pytest.mark.parametrize allows to define parametrization at the function or class level, provides multiple argument/fixture sets for a particular test function or class.

  • pytest_generate_tests enables implementing your own custom dynamic parametrization scheme or extensions.

几个例子 here .

关于python - 如何为pytest传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30661605/

相关文章:

python - Python 中最简单的异步/等待示例

ios - XCTest 与 SenTestingKit 相比有哪些优势?

javascript - 在代码中启动 Devbridge 自动完成插件搜索

Python:函数的单元测试不返回任何东西,但改变对象的状态

javascript - [React-Native][Jest]SyntaxError : Unexpected token import

python - div 在使用 BeautifulSoup 抓取价格时返回空,其他所有内容(如标题、图片链接)返回一个值。为什么会发生这种情况?

python - 什么是 'NoneType' 对象?

python - 根据唯一值拆分嵌套列表

unit-testing - 如何测试 ioutil.ReadFile 和 os.Stat?

java - 在方法内部调用方法时模拟异常