Skip to main content

Quick and easy server mocking.

Project description

Quick and easy server mocking.

Installation

Via github:

~$ git clone http://github.com/bprinty/faux.git
~$ cd faux
~$ python setup.py install

Via pip:

~$ pip install faux

Documentation

Documentation for the package can be found here.

Usage

The faux provides utilities for mocking responses from external services during testing. With faux, you can easily serve a directory structure mocking url endpoints for an externally managed service and use that server for testing.

High-Level

For instance, if you have a directory structure that looks like the following:

├── _uuid
├── file
└── query/
    ├── data
    └── arg=test

With the following as contents of the files in that directory structure:

# _uuid
{
    "status": "ok",
    "city": "{{city}}"
}

# file
{
    "status": "ok",
    "month": "{{month}}",
}

# query/arg=test
{
    "status": "ok",
    "arg": "test",
    "digit": {{random_digit}}
}

# query/data
{
    "status": "ok",
    "data": "test"
}

Endpoints mirroring that file structure will be available:

>>> import requests
>>> r = requests.get('http://localhost:1234/4db5fd8c-8aa6-4c29-b979-dab3ce71e64e')
>>> print(r.json())
{
    "status": "ok",
    "city": "Sacramento",
}

>>> r = requests.get('http://localhost:1234/file')
>>> print(r.json())
{
    "status": "ok",
    "month": "05"
}

>>> r = requests.get('http://localhost:1234/query?arg=test')
>>> print(r.json())
{
    "status": "ok",
    "arg": "test",
    "digit": 4
}

>>> r = requests.get('http://localhost:1234/query/data')
>>> print(r.json())
{
    "status": "ok",
    "data": "test"
}

It’s also worth noting (alluded to above) that you can mock arbitrary data in your responses using methods from the faker library. Items like {{city}} and {{month}} above were automatically and randomly filled without outputs from a faker.Faker() object during the request.

One other special file above is the _uuid file, which will return data from the _uuid file whenever a uuid is included as part of the request.

Starting a Server

For the previous example, you can start the server on a specific port using:

.. code-block:: bash

~$ faux serve -P 1234 /path/to/directory

Using Within Tests

One of the most common paradigms for using this software is to mock a service during testing. To do so with this module, you can easily set up a py.test fixture that will run throughout your test session:

import unittest
import pytest

RESOURCES = '/path/to/testing/resources'

@pytest.fixture(scope='session')
def server():
    """
    Set up mock server for testing request caching.
    """
    from faux import Server
    app = Server(__name__, cache=RESOURCES)
    with app.run(port=1234):
        yield
    return

Once you’ve defined the fixture, you can use it on a test class or function like so:

# test function
@pytest.mark.usefixtures("server")
def test_function():
    return


# test class
@pytest.mark.usefixtures("server")
class TestClass(unittest.TestCase):
    def test_method():
        return

With the code above, the server you’re mocking will run throughout your testing session and will gracefully exit when the test session stops.

Other Functionality

To see other functionality provided by the library, please see the documentation.

Questions/Feedback

File an issue in the GitHub issue tracker.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

faux-0.0.7.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

faux-0.0.7-py2.py3-none-any.whl (10.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file faux-0.0.7.tar.gz.

File metadata

  • Download URL: faux-0.0.7.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.5.2

File hashes

Hashes for faux-0.0.7.tar.gz
Algorithm Hash digest
SHA256 5bff9d7d8c843f6f54128c894180627a379a1ef46c747a5dce77ddc007af84cd
MD5 b6772232f112dea5e4b802e46614a12e
BLAKE2b-256 204cddc7b629652754b59a4feb4a3909affa6b528f14678f3d7a7107d0662932

See more details on using hashes here.

File details

Details for the file faux-0.0.7-py2.py3-none-any.whl.

File metadata

  • Download URL: faux-0.0.7-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.5.2

File hashes

Hashes for faux-0.0.7-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e3c67840040e96b5101cdf699374c24d329f3d72ef1615a7e8577421da288a72
MD5 167df38d7db5a3a8358e023ef6d4a922
BLAKE2b-256 c716b213f696b3b2e6f885ff0da7f96c91b60ee8d0928ff1d52ebc28f9f2f3e5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page