Skip to main content

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.

Release files for faux 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for faux 0.0.7
File Size Uploaded
faux-0.0.7.tar.gz 21.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for faux 0.0.7
File Interpreter ABI Platform
faux-0.0.7-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 32.0 kB

Release files / faux-0.0.7.tar.gz

Download URL faux-0.0.7.tar.gz
Size 21.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5bff9d7d8c843f6f54128c894180627a379a1ef46c747a5dce77ddc007af84cd
BLAKE2b-256 checksum
How to use checksums
204cddc7b629652754b59a4feb4a3909affa6b528f14678f3d7a7107d0662932
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / faux-0.0.7-py2.py3-none-any.whl

Download URL faux-0.0.7-py2.py3-none-any.whl
Size 10.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
e3c67840040e96b5101cdf699374c24d329f3d72ef1615a7e8577421da288a72
BLAKE2b-256 checksum
How to use checksums
c716b213f696b3b2e6f885ff0da7f96c91b60ee8d0928ff1d52ebc28f9f2f3e5
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page