Skip to content

Using requests within tests using pyfakefs #674

Description

@davidorme

Hi,

This an odd use case failure and not a bug but I just wanted to record it in case anyone else comes it.

We are using the requests package within a set of tests using pyfakefs. That gave us a consistent failure:

===short test summary info 
FAILED test/test_requests.py::test_requests_fake - OSError: Could not find a suitable TLS CA certificate bundle, invalid path: /xxx/site-packages/cert...
===1 failed, 1 passed in 2.33s 

The issue here is that requests uses the certifi package for HTTPS certificate chains - and the underlying certificate bundle is only loaded from file when required, not when certifi is imported. So, tests that use requests from HTTPS sites fail.

Once we figured out the problem, the solution is pretty simple: in the setup of the fake filesystem (probably as a fixture in conftest.py) you need to import certifi and then add the real certificate bundle to fs.

import certifi
fs.add_real_file(certifi.where())

I don't think any changes are needed - unless there is something that allows fs to know about site package locations automatically - but it took us a while to figure out what was wrong.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions