1

I have following code:

@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

However I'm getting following error:

In test_subscrition_handle_from_file_when_file_is_not_present: function uses no argument 'file_exists'

How I can specify parameters to mock_path_function()?

4
  • 1
    You need to swap the position of your decorators.! Commented Jul 5, 2019 at 7:03
  • Possible duplicate of How to parametrize a Pytest fixture Commented Jul 5, 2019 at 7:29
  • Thanks, changed position of arguments but same result Commented Jul 5, 2019 at 7:40
  • I checked "How to Parametrize" SF post, but I haven't found a solution to my problem Commented Jul 5, 2019 at 7:43

2 Answers 2

0

I think you just need to pass your arguments into your test function, like

@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
Sign up to request clarification or add additional context in comments.

Comments

0

I got the same error below with pytest-django and pytest-factoryboy in Django:

Failed: In test_user_instance: function uses no argument 'email'

Because I did not put email argument to test_user_instance() even though I set email to @pytest.mark.parametrize() as shown below:

import pytest
from django.contrib.auth.models import User

@pytest.mark.parametrize(
    "username, email", # Here
    [
        ("John", "[email protected]"),
        ("David", "[email protected]")
    ]
)
def test_user_instance(
    db, user_factory, username, # email # Here
):
    user_factory(
        username=username,
        # email=email
    )

    item = User.objects.all().count()
    assert item == True

So, I put email argument to test_user_instance() as shown below, then the error was solved:

import pytest
from django.contrib.auth.models import User

@pytest.mark.parametrize(
    "username, email",
    [
        ("John", "[email protected]"),
        ("David", "[email protected]")
    ]
)
def test_user_instance(
    db, user_factory, username, email # Here
):
    user_factory(
        username=username,
        email=email
    )

    item = User.objects.all().count()
    assert item == True

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.