-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Using monkeypatch in non function scoped fixture #363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42): depends on what you wants to do exactly but here is a workaround using internal objects:
|
I would like to I am getting the following issue when I attempt to do so:
with this code (just switched the names around): @pytest.fixture
def fixture_data(some_data, monkeypatch):
class MyDate(object):
@classmethod
def now(cls):
return 'date'
monkeypatch.setattr(
field_values,
'datetime',
MyDate,
)
return func_to_test(some_data) An equivalent with python's import mock
import unittest
import module_to_test
class MyDate(object):
@classmethod
def now(cls):
return 'gibberish'
class MyTestCase(unittest.TestCase):
@classmethod
@mock.patch.object(module_to_test, 'datetime', MyDate)
def setUpClass(self):
self.test_data = func_to_test(dummy_data)
def test_func_to_test(self):
self.assertEqual(self.test_data['create_date'], 'gibberish') While this "works", I am not able to make this run through the module. I had hoped that it was my inexperience with the library, till I found this issue. Is there anything I can do to help with this? Or is it actually my inexperience with the library? :) I can point to actual code if desired, I didn't want to swamp maintainers/triagers with too much info. |
New Features ========== - EmailField, DateTimeField now supported Refactors ======= - Refactored helper methods out to their own class - Added test cases and refactored tests into smaller functions - Requirements have been reorganized Enhancements =========== - travis.ml has been added to test against Python 2.7, other versions will be added during packaging. Technical Debt =========== Still unable to make the fixture where the date is being patched run in a scope beyond the function scope. Ideally, one would want a module, or even session scope. This issue is logged on the py.test repo since 2013, a comment has been logged before this commit with a follow up. pytest-dev/pytest#363
New Features ========== - EmailField, DateTimeField now supported Refactors ======= - Refactored helper methods out to their own class - Added test cases and refactored tests into smaller functions - Requirements have been reorganized Enhancements =========== - travis.ml has been added to test against Python 2.7, other versions will be added during packaging. Technical Debt =========== Still unable to make the fixture where the date is being patched run in a scope beyond the function scope. Ideally, one would want a module, or even session scope. This issue is logged on the py.test repo since 2013, a comment has been logged before this commit with a follow up. pytest-dev/pytest#363
Hi @agamdua, Fixtures can only use fixtures of same scope or broader (for example, Did you notice @hpk42 response just before your question? It contains an appropriate workaround to what you are trying to accomplish. If you prefer to use the @pytest.yield_fixture
def fixture_data(some_data):
class MyDate(object):
@classmethod
def now(cls):
return 'date'
with mock.patch.object(field_values, 'datetime', new=MyDate()):
yield func_to_test(some_data) Also, if you like the mock package, checkout pytest-mock. |
Hey! Thanks for the response.
I did notice the other response - I would have ideally liked to abstract the Thanks also for the example with mock, and pytest-mock packages. The first I didn't think of, and the second I didn't know about.
I will look go through #797 as well, and see if there's anything that comes to mind. |
The solution above isn't working with Instead I have changed it to @pytest.fixture(scope="session")
def monkeysession(request):
mpatch = MonkeyPatch()
yield mpatch
mpatch.undo() Am i doing this right? |
this is indeed currently necessary, we had to roll back the feature due to a harsh regression |
Is it possible to add @kunalbhagawati's code block to a |
@bossjones that's a good idea. Would you like to contribute a PR? 😉 |
Good call, i'll do that :) @nicoddemus |
Is kunalbhagawati's solution still working, or is there now another workaround? I'm having trouble implementing it in my project – |
@lukewrites it should still be working, nothing has changed in monkeypatch since then. Can you post the full traceback please? |
Just a note, to use kunal's solution, you need
|
@nicoddemus: Would adding the I'd help out with a PR to that effect, but... well, I'm lazy. (Also, overworked and underpaid.) |
👍 for shipping |
Why do the pytest docs say you can use |
Hmm. They also say "Note that the ability to use a monkeypatch fixture from a session-scoped fixture was added in pytest-3.0." This comment indicates that feature was removed. Probably the documentation is stale. Also, should this issue be re-opened if the feature was rolled back? |
Aah, you were looking at the pytest 3.0.1 docs. The latest docs don't mention that feature. |
Ah apologies, that's what I get for following Google |
Hi, I don't see invocation-scoped fixtures being implemented at all anytime soon, we expect a good refactoring of the fixture implementation until that can happen. Given that the implementation of Just thought I would mention: we had the same issue for years with I mention this just because it came to mind, I'm OK with |
monkeypatch_session should come with documentation ensuring it will at some point be reconciled a monkeypatch factory/root doesnt make sense as of now (per request would be a nice way for monkeypatch (so every fixture that uses it gets a own one matching its scope) |
We want to export `pytest.MonkeyPatch` for the purpose of type-annotating the `monkeypatch` fixture. For other fixtures we export in this way, we also make direct construction of them (e.g. `MonkeyPatch()`) private. But unlike the others, `MonkeyPatch` is also widely used directly already, mostly because the `monkeypatch` fixture only works in `function` scope (issue pytest-dev#363), but also in other cases. So making it private will be annoying and we don't offer a decent replacement yet. So, let's just make direct construction public & documented.
We want to export `pytest.MonkeyPatch` for the purpose of type-annotating the `monkeypatch` fixture. For other fixtures we export in this way, we also make direct construction of them (e.g. `MonkeyPatch()`) private. But unlike the others, `MonkeyPatch` is also widely used directly already, mostly because the `monkeypatch` fixture only works in `function` scope (issue pytest-dev#363), but also in other cases. So making it private will be annoying and we don't offer a decent replacement yet. So, let's just make direct construction public & documented.
We want to export `pytest.MonkeyPatch` for the purpose of type-annotating the `monkeypatch` fixture. For other fixtures we export in this way, we also make direct construction of them (e.g. `MonkeyPatch()`) private. But unlike the others, `MonkeyPatch` is also widely used directly already, mostly because the `monkeypatch` fixture only works in `function` scope (issue pytest-dev#363), but also in other cases. So making it private will be annoying and we don't offer a decent replacement yet. So, let's just make direct construction public & documented.
We want to export `pytest.MonkeyPatch` for the purpose of type-annotating the `monkeypatch` fixture. For other fixtures we export in this way, we also make direct construction of them (e.g. `MonkeyPatch()`) private. But unlike the others, `MonkeyPatch` is also widely used directly already, mostly because the `monkeypatch` fixture only works in `function` scope (issue pytest-dev#363), but also in other cases. So making it private will be annoying and we don't offer a decent replacement yet. So, let's just make direct construction public & documented.
We want to export `pytest.MonkeyPatch` for the purpose of type-annotating the `monkeypatch` fixture. For other fixtures we export in this way, we also make direct construction of them (e.g. `MonkeyPatch()`) private. But unlike the others, `MonkeyPatch` is also widely used directly already, mostly because the `monkeypatch` fixture only works in `function` scope (issue pytest-dev#363), but also in other cases. So making it private will be annoying and we don't offer a decent replacement yet. So, let's just make direct construction public & documented.
Per advice in pytest-dev/pytest#363 (comment)
As of pytest 6.2, this is now quite a bit simpler with no need for a private import, thanks to the @pytest.fixture(scope="session")
def monkeysession():
with pytest.MonkeyPatch.context() as mp:
yield mp |
IMO this ought to be available as a fixture out of the box since I seem to be using it a lot, and this feels like code to be re-used. |
Originally reported by: BitBucket: ColeVsCode, GitHub: ColeVsCode
Hi, I'd like to be able to create a module or session scoped fixture that uses monkeypatch.
This raises an error. There's some comments about this on the pytest-dev list.
https://mail.python.org/pipermail/pytest-dev/2012-November/002157.html
Is there a work-around in the mean time that I can use to change the scope of the monkeypatch fixture?
The text was updated successfully, but these errors were encountered: