|
| 1 | + |
| 2 | +.. _fixtures: |
| 3 | + |
| 4 | +Fixtures |
| 5 | +======== |
| 6 | + |
| 7 | +pytest-httpserver provides the following pytest fixtures. These fixtures can be |
| 8 | +overridden the usual name, by defining a fixture with the same name. Make sure |
| 9 | +that you are defining the fixture with the same scope as the original one. For |
| 10 | +more details, read the related part of the `pytest howto`_. |
| 11 | + |
| 12 | +httpserver |
| 13 | +---------- |
| 14 | + |
| 15 | +Scope |
| 16 | + function |
| 17 | + |
| 18 | +Type |
| 19 | + :py:class:`pytest_httpserver.HTTPServer` |
| 20 | + |
| 21 | + |
| 22 | +This fixture provides the main functionality for the library. It is a httpserver |
| 23 | +instance where you can add your handlers and expectations. It is a function |
| 24 | +scoped fixture as the server's state needs to be cleared between the tests. |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +httpserver_listen_address |
| 29 | +------------------------- |
| 30 | + |
| 31 | +Scope |
| 32 | + session (in 1.0.0 and above, *function* otherwise) |
| 33 | + |
| 34 | +Type: |
| 35 | + ``Tuple[str, int]`` |
| 36 | + |
| 37 | +Default: |
| 38 | + ``("localhost", 0)`` |
| 39 | + |
| 40 | +This fixture can return the address and port where the server will bind. If port |
| 41 | +is given is 0, the server to an ephemeral port, which is an available randomly |
| 42 | +selected port. If you run your tests in parallel, this should be used so |
| 43 | +multiple servers can be started. |
| 44 | + |
| 45 | + |
| 46 | +httpserver_listen_address |
| 47 | + |
| 48 | + |
| 49 | +httpserver_ssl_context |
| 50 | +---------------------- |
| 51 | +Scope |
| 52 | + session |
| 53 | + |
| 54 | +Type: |
| 55 | + ``ssl.SSLContext`` |
| 56 | + |
| 57 | +Default: |
| 58 | + ``None`` |
| 59 | + |
| 60 | + |
| 61 | +This fixture should return the ssl context which will be used to run a https |
| 62 | +server. For more details please see the `ssl`_ module documentation of the |
| 63 | +standard library. |
| 64 | + |
| 65 | + |
| 66 | +make_httpserver |
| 67 | +--------------- |
| 68 | +Scope |
| 69 | + session |
| 70 | + |
| 71 | +Type: |
| 72 | + :py:class:`pytest_httpserver.HTTPServer` |
| 73 | + |
| 74 | +Default: |
| 75 | + A running :py:class:`pytest_httpserver.HTTPServer` instance. |
| 76 | + |
| 77 | + |
| 78 | +This is a factory fixture which creates the instance of the httpserver which |
| 79 | +will be used by the ``httpserver`` fixture. By default, it uses the |
| 80 | +``httpserver_listen_address`` and the ``httpserver_ssl_context`` fixtures but |
| 81 | +can be overridden to add more customization. |
| 82 | + |
| 83 | +It yields a running HTTPServer instance and also stops it when it is no longer |
| 84 | +needed at the end of the session. If you want to customize this fixture it is |
| 85 | +highly recommended to look at its definition in `pytest_plugin.py`_. |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +.. _pytest_plugin.py: |
| 90 | + https://github.com/csernazs/pytest-httpserver/blob/master/pytest_httpserver/pytest_plugin.py |
| 91 | + |
| 92 | +.. _pytest howto: |
| 93 | + https://docs.pytest.org/en/documentation-restructure/how-to/fixture.html#overriding-fixtures-on-various-levels |
| 94 | + |
| 95 | +.. _ssl: |
| 96 | + https://docs.python.org/3/library/ssl.html |
0 commit comments