Skip to content
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

Document how to configure options on the whole test suite #154

Closed
Apakottur opened this issue Sep 23, 2024 · 6 comments
Closed

Document how to configure options on the whole test suite #154

Apakottur opened this issue Sep 23, 2024 · 6 comments
Labels
documentation Improvements or additions to documentation

Comments

@Apakottur
Copy link
Contributor

We are using the assert_all_responses_were_requested option in our test suite to avoid raising an error if not all responses were requested.
We have thousands of unit tests across hundreds of test files that would error without this flag being set to False so we were using the fixture to set it to false globally for the entire test suite.

The changes in #145 now make this flag scoped, which is a great feature, but not applicable in our case, meaning we can't use the newest version of the package.

It would be great to still be able to configure it once for the entire test suite (otherwise we'd have to add it to hundreds of files).

Or perhaps it's possible to configure it globally for entire test suite and I'm just missing something?

@Colin-b
Copy link
Owner

Colin-b commented Sep 23, 2024

Hello @Apakottur ,

I am not proficient enough with pytest marks to give you a definitive answer on this.
Maybe @RazerM would know if such option exists?

If not, I am sure we could cook something up.

If you are using this option because you have a shared fixture registering a lot of responses that might not be useful in all tests, you could have a look at calling the following method at teardown (after the yield statement of your fixture).

httpx_mock.reset()

This would however prevent any assertion performed at teardown by httpx_mock (your test suite would not fail at teardown if some HTTP calls were not mocked, but your tests might still fail depending on how your code handle the Timeout raised by httpx_mock when nothing was registered).

@Colin-b Colin-b added the question Further information is requested label Sep 23, 2024
@RazerM
Copy link
Contributor

RazerM commented Sep 23, 2024

You could add this to your root conftests.py to add the marker to every test:

def pytest_collection_modifyitems(session, config, items):
    for item in items:
        item.add_marker(pytest.mark.httpx_mock(assert_all_responses_were_requested=False))

@RazerM
Copy link
Contributor

RazerM commented Sep 23, 2024

Also maybe we should change

marker = request.node.get_closest_marker("httpx_mock")

to use request.node.iter_markers("httpx_mock") so that we appropriately combine them, e.g.

pytestmark = pytest.mark.httpx_mock(assert_all_responses_were_requested=False)

@pytest.mark.httpx_mock(non_mocked_hosts=["example.com"])
def test_foo():
    ...

@Colin-b
Copy link
Owner

Colin-b commented Sep 23, 2024

Nice, so there is a workaround.

Indeed we should fix the way we currently handle provided options.
Feel free to draft a PR (or I'll do it whenever I find some more time :)), I opened an issue for it: #155

And this ticket is now a documentation update one

@Colin-b Colin-b changed the title Configuring options for entire test suite Document how to configure options on the whole test suite Sep 23, 2024
@Colin-b Colin-b added documentation Improvements or additions to documentation and removed question Further information is requested labels Sep 23, 2024
@Apakottur
Copy link
Contributor Author

You could add this to your root conftests.py to add the marker to every test:

def pytest_collection_modifyitems(session, config, items):
    for item in items:
        item.add_marker(pytest.mark.httpx_mock(assert_all_responses_were_requested=False))

This works perfectly for our usecase, thank you!

@Colin-b Colin-b mentioned this issue Sep 23, 2024
@Colin-b
Copy link
Owner

Colin-b commented Sep 23, 2024

Documentation was updated to help setting this up and code now handles multiple level of configuration

@Colin-b Colin-b closed this as completed Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants