-
Notifications
You must be signed in to change notification settings - Fork 13
Update README to provide more clear examples #105
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,15 +29,48 @@ no further! | |
Quickstart | ||
========== | ||
|
||
`myproject.py` | ||
|
||
.. code:: python | ||
|
||
import requests | ||
|
||
|
||
def make_request(url): | ||
"""A function that makes a web request.""" | ||
rsp = requests.get(url) | ||
return rsp.text | ||
|
||
`test_myproject.py` | ||
|
||
.. code:: python | ||
|
||
from myproject import request | ||
|
||
|
||
def test_make_request(httpserver): | ||
httpserver.serve_content( | ||
content="success", | ||
code=200, | ||
) | ||
|
||
assert make_request(httpserver.url) == "success" | ||
|
||
How-To | ||
====== | ||
|
||
Let's say you have a function to scrape HTML which only required to be pointed | ||
at a URL :: | ||
at a URL : | ||
|
||
.. code:: python | ||
|
||
import requests | ||
|
||
def scrape(url): | ||
html = requests.get(url).text | ||
# some parsing happens here | ||
# ... | ||
return result | ||
html = requests.get(url).text | ||
# some parsing happens here | ||
# ... | ||
return result | ||
|
||
You want to test this function in its entirety without having to rely on a | ||
remote server whose content you cannot control, neither do you want to waste | ||
|
@@ -46,29 +79,39 @@ modules dealing with the actual HTTP request (of which there are more than one | |
BTW). So what do you do? | ||
|
||
You simply use pytest's `funcargs feature`_ and simulate an entire server | ||
locally! :: | ||
locally! : | ||
alissa-huskey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
.. code:: python | ||
|
||
def test_retrieve_some_content(httpserver): | ||
httpserver.serve_content(open('cached-content.html').read()) | ||
assert scrape(httpserver.url) == 'Found it!' | ||
httpserver.serve_content(open("cached-content.html").read()) | ||
assert scrape(httpserver.url) == "Found it!" | ||
|
||
What happened here is that for the duration of your tests an HTTP server is | ||
started on a random port on localhost which will serve the content you tell it | ||
to and behaves just like the real thing. | ||
|
||
The added bonus is that you can test whether your code behaves gracefully if | ||
there is a network problem:: | ||
there is a network problem: | ||
|
||
def test_content_retrieval_fails_graciously(httpserver): | ||
httpserver.serve_content('File not found!', 404) | ||
pytest.raises(ContentNotFoundException, scrape, httpserver.url) | ||
.. code:: python | ||
|
||
The same thing works for SMTP servers, too:: | ||
def test_content_retrieval_fails_graciously(httpserver): | ||
httpserver.serve_content("File not found!", 404) | ||
pytest.raises(ContentNotFoundException, scrape, httpserver.url) | ||
|
||
The same thing works for SMTP servers, too: | ||
|
||
.. code:: python | ||
|
||
def test_sending_some_message(smtpserver): | ||
mailer = MyMailer(host=smtpserver.addr[0], port=smtpserver.addr[1]) | ||
mailer.send(to='[email protected]', from_='[email protected]', | ||
subject='MyMailer v1.0', body='Check out my mailer!') | ||
mailer.send( | ||
to="[email protected]", | ||
from_="[email protected]", | ||
subject="MyMailer v1.0", | ||
body="Check out my mailer!" | ||
diazona marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
) | ||
assert len(smtpserver.outbox)==1 | ||
|
||
Here an SMTP server is started which accepts e-mails being sent to it. The | ||
|
@@ -77,11 +120,12 @@ and what was sent by looking into the smtpserver's ``outbox``. | |
|
||
It is really that easy! | ||
|
||
Available funcargs | ||
================== | ||
Fixtures | ||
======== | ||
|
||
Here is a short overview of the available funcargs. For more details I suggest | ||
poking around in the code itself. | ||
Here is a short overview of the available pytest fixtures and their usage. This | ||
information is also available via `pytest --fixtures`. For more details I | ||
suggest poking around in the code itself. | ||
|
||
``httpserver`` | ||
provides a threaded HTTP server instance running on localhost. It has the | ||
|
@@ -95,9 +139,17 @@ poking around in the code itself. | |
|
||
Once these attributes are set, all subsequent requests will be answered with | ||
these values until they are changed or the server is stopped. A more | ||
convenient way to change these is :: | ||
convenient way to change these is : | ||
|
||
.. code:: python | ||
|
||
httpserver.serve_content(content=None, code=200, headers=None, chunked=pytest_localserver.http.Chunked.NO, store_request_data=True) | ||
httpserver.serve_content( | ||
content=None, | ||
code=200, | ||
headers=None, | ||
chunked=pytest_localserver.http.Chunked.NO, | ||
store_request_data=True | ||
alissa-huskey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
) | ||
|
||
The ``chunked`` attribute or parameter can be set to | ||
|
||
|
@@ -149,27 +201,37 @@ Using your a WSGI application as test server | |
============================================ | ||
|
||
As of version 0.3 you can now use a `WSGI application`_ to run on the test | ||
server :: | ||
|
||
from pytest_localserver.http import WSGIServer | ||
|
||
def simple_app(environ, start_response): | ||
"""Simplest possible WSGI application""" | ||
status = '200 OK' | ||
response_headers = [('Content-type', 'text/plain')] | ||
start_response(status, response_headers) | ||
return ['Hello world!\n'] | ||
|
||
@pytest.fixture | ||
def testserver(request): | ||
"""Defines the testserver funcarg""" | ||
server = WSGIServer(application=simple_app) | ||
server.start() | ||
request.addfinalizer(server.stop) | ||
return server | ||
|
||
def test_retrieve_some_content(testserver): | ||
assert scrape(testserver.url) == 'Hello world!\n' | ||
server : | ||
|
||
.. code:: python | ||
|
||
import pytest | ||
from pytest_localserver.http import WSGIServer | ||
|
||
from myproject import make_request | ||
|
||
|
||
def simple_app(environ, start_response): | ||
"""Respond with success.""" | ||
status = "200 OK" | ||
response_headers = [("Content-type", "text/plain")] | ||
start_response(status, response_headers) | ||
return ["success".encode("utf-8")] | ||
|
||
|
||
@pytest.yield_fixture | ||
alissa-huskey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
def testserver(): | ||
"""Server for simple_app.""" | ||
server = WSGIServer(application=simple_app) | ||
server.start() | ||
yield server | ||
server.stop() | ||
|
||
|
||
def test_make_request(testserver): | ||
"""make_request() should return "success".""" | ||
assert make_request(testserver.url) == "success" | ||
|
||
|
||
Have a look at the following page for more information on WSGI: | ||
http://wsgi.readthedocs.org/en/latest/learn.html | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.