我有以下代码:
@pytest.fixture
def mock_path_functions(mocker, file_exists=True, file_size=10):
mock_obj = mock.Mock()
mock_obj.st_size = file_size
mocker.patch("os.path.exists", return_value=file_exists)
mocker.patch("os.stat", return_value=mock_obj)
@pytest.mark.usefixtures("mock_path_functions")
@ytest.mark.parametrize("file_exists,file_size", [(False, 0)])
def test_subscrition_handle_from_file_when_file_is_not_present():
assert subscrition_handle_from_file("some path") == None
但是我收到以下错误:
In test_subscrition_handle_from_file_when_file_is_not_present: function uses no argument 'file_exists'
如何指定要mock_path_function()
的参数?
我认为你只需要将你的参数传递到你的测试函数中,比如
@pytest.mark.usefixtures("mock_path_functions")
@ytest.mark.parametrize("file_exists,file_size", [(False, 0)])
def test_subscrition_handle_from_file_when_file_is_not_present(
file_exists, file_size
):
assert subscrition_handle_from_file("some path") == None
我在下面遇到了同样的错误,在 Django 中使用 pytest-django 和 pytest-factoryboy:
失败:在test_user_instance:函数不使用参数"电子邮件">
因为我没有把email
参数放在test_user_instance()
即使我email
设置为 @pytest.mark.parametrize() 如下所示:
import pytest
from django.contrib.auth.models import User
@pytest.mark.parametrize(
"username, email", # Here
[
("John", "test1@test1.com"),
("David", "test2@test2.com")
]
)
def test_user_instance(
db, user_factory, username, # email # Here
):
user_factory(
username=username,
# email=email
)
item = User.objects.all().count()
assert item == True
因此,我将email
参数放在test_user_instance()
如下所示,然后错误得到解决:
import pytest
from django.contrib.auth.models import User
@pytest.mark.parametrize(
"username, email",
[
("John", "test1@test1.com"),
("David", "test2@test2.com")
]
)
def test_user_instance(
db, user_factory, username, email # Here
):
user_factory(
username=username,
email=email
)
item = User.objects.all().count()
assert item == True