mocker fixture usage - assistance please #7789
-
Trying to come to terms with mocking AND pytest test code from 2020 youtube -> @nikhilkumarsingh tut9 from unittest import mock
import pytest
from tut9.myapp.sample import guess_number
@pytest.mark.parametrize("_input,expected", [(3, "You won!"), (4, "You lost!")])
@mock.patch("tut9.myapp.sample.roll_dice")
def test_guess_number(mock_roll_dice, _input, expected):
mock_roll_dice.return_value = 3
assert guess_number(_input) == expected
mock_roll_dice.assert_called_once() but when I try to code it to use mocker import pytest
from tut9.myapp.sample import guess_number
@pytest.mark.parametrize("in_put,out_put", [(3, "You won!"), (4, "You lost!")])
def test_guess_num(mocker, in_put, out_put):
mocker.patch("tut9.myapp.sample.roll_dice", return_value=3)
assert out_put == guess_number(in_put)
#tut9.myapp.sample.roll_dice.assert_called_once() the above partial conversion has taken me hours i have been using the IDE intellisense I have no idea as to how to implement all I can see are redirects to I might be old and senile now what about the teen pythonites |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments
-
https://docs.pytest.org/en/stable/search.html?q=mocker <div id="search-results">
<h2>Search Results</h2>
<p class="search-summary">Your search did not match any documents. Please make sure that all words are spelled correctly and that you've selected enough categories.</p>
<ul class="search"></ul>
</div> |
Beta Was this translation helpful? Give feedback.
-
Hi @kevindawson,
The import pytest
from tut9.myapp.sample import guess_number
@pytest.mark.parametrize("_input,expected", [(3, "You won!"), (4, "You lost!")])
def test_guess_number(mocker, _input, expected):
mock_roll_dice = mock.patch("tut9.myapp.sample.roll_dice")
mock_roll_dice.return_value = 3
assert guess_number(_input) == expected
mock_roll_dice.assert_called_once() Particularly I find using the decorator facilities of |
Beta Was this translation helpful? Give feedback.
-
@nicoddemus mock_roll_dice = mock.patch("tut9.myapp.sample.roll_dice")
mock_roll_dice.return_value=3 OR mock_roll_dice = mocker.patch("tut9.myapp.sample.roll_dice", return_value=3) Also in your Why bother with a plugin? you seam to be saying what not to do, rather than you could do this with an example |
Beta Was this translation helpful? Give feedback.
-
My bad. Both will work though (updated the original post).
There I try to exemplify how one would use |
Beta Was this translation helpful? Give feedback.
-
@nicoddemus 4, I found some documentation: Data Pipelines with Apache Airflow 8.1.4 pytest project structure. Cool |
Beta Was this translation helpful? Give feedback.
-
Why do you ask? If you see something strange, please point it out, no need to make me review it entirely.
Hence what over to me? |
Beta Was this translation helpful? Give feedback.
-
1, I think you should have Assign mock to a variable, enables capture behaviour mock_roll_dice = mock.patch("tut9.myapp.sample.roll_dice") 2, @nicoddemus you are the author are you not. you have written a cool plugin because you are a cool dude, but you have forgotten to turn the light on for all the new users, especially the teen pythonites 3, I will ask @nikhilkumarsingh about using his code so you can have some examples for us mere mortals when we first come across something NEW |
Beta Was this translation helpful? Give feedback.
-
Ahh thanks. Fixed. @kevindawson can you mark my first post as answering your question if that's the case? This will help others find the answer quickly. Thanks. 👍 |
Beta Was this translation helpful? Give feedback.
-
@nicoddemus I have pondered over this for some time now: 1, the use of
2, Assign mock to a variable, enables capture behaviour
3, the use of the helper method
All of the above goes into the following: @pytest.mark.parametrize("in_put,out_put", [(3, "You won!"), (4, "You lost!")])
def test_guess_number(mocker, in_put, out_put):
loaded_dice = mocker.patch("tut9.myapp.sample.roll_dice", return_value=3)
assert guess_number(in_put) == out_put
assert loaded_dice.call_count == 1 |
Beta Was this translation helpful? Give feedback.
@nicoddemus I have pondered over this for some time now:
1, the use of
_input
to avoid stomping ove the python built-in function(input)input_
both of which I do not like2, Assign mock to a variable, enables capture behaviour
mock_roll_dice
as perunittest
pytest-mock
let's us define the variable name we could be more descriptiveloaded_dice
in this case3, the use of the helper method
assert_called_once()
call_count
and see it incremen…