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

Update README to provide more clear examples #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 104 additions & 42 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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! :

.. 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!"
)
assert len(smtpserver.outbox)==1

Here an SMTP server is started which accepts e-mails being sent to it. The
Expand All @@ -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
Expand All @@ -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
)

The ``chunked`` attribute or parameter can be set to

Expand Down Expand Up @@ -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
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
Expand Down