python - 使用 pytest 测试类方法 - 错误 - 找不到夹具“自我”
问题描述
我想测试一个类方法,py.test
我已经按照这篇文章使用 pytest 测试类方法。
我的方法有calgo.py
一个文件test_calgo.py
(learning_ut
当我运行 pytest 时,我收到一条错误消息说fixture 'self' not found
.
我哪里错了?
谢谢!
# calgo.py
class MyClass():
def func(self, x):
return x+1
# test_calgo.py
import calgo
def test_func(self):
mc = MyClass()
assert mc.func(3) == 4
# Command line
(pyenv) C:\Users\Jimmy\Desktop\learning_ut>pytest
# Error message
(pyenv) C:\Users\Jimmy\Desktop\learning_ut>pytest
================================================= test session starts =================================================
platform win32 -- Python 3.6.13, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: C:\Users\Jimmy\Desktop\learning_ut
collected 1 item
test_calgo.py E [100%]
======================================================= ERRORS ========================================================
_____________________________________________ ERROR at setup of test_func _____________________________________________
file C:\Users\Jimmy\Desktop\learning_ut\test_calgo.py, line 11
def test_func(self):
E fixture 'self' not found
> available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary,
doctest_namespace, monkeypatch, pytestconfig, record_property, record_testsuite_property,
record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
C:\Users\Jimmy\Desktop\learning_ut\test_calgo.py:11
=============================================== short test summary info ===============================================
ERROR test_calgo.py::test_func
解决方案
我不知道pytest
,在unittest
您创建一个测试类时,该类将具有属于此类的方法并具有self
作为参数。但是,您的错误似乎是由 in 中的函数引起的,该函数calgo.py
不属于某个类,因此没有自引用。尝试以下操作:
import calgo
def test_func():
mc = MyClass()
assert mc.func(3) == 4
推荐阅读
- r - R中是否有合并2个向量的特定函数
- java - 如何在 Spring 中按另一个实体的属性排序?
- python - 安装 CUDA 后无法检测 GPU
- javascript - 为什么 sprite.tintBottomLeft 在移相器 3 中返回错误的颜色?
- flutter - Flutter - 任务“:app:multiDexListDebug”执行失败
- reactjs - 如何“禁用”用antV库制作的图表图例文本中的自动换行?
- git - 如何从强制推送的分支中提取更新?
- amazon-web-services - AWS Lambda 为定期执行的函数保留其执行上下文多长时间?
- jmeter - 如何从摘要报告中识别吞吐量
- amazon-web-services - “开发人员”和“供应官员”之间的访问隔离