
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Quick and easy server mocking.
Via github:
.. code-block:: bash
~$ git clone http://github.com/bprinty/faux.git
~$ cd faux
~$ python setup.py install
Via pip:
.. code-block:: bash
~$ pip install faux
Documentation for the package can be found here <http://faux.readthedocs.io/en/latest/index.html>
_.
The faux <http://github.com/bprinty/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:
.. code-block::
├── _uuid
├── file
└── query/
├── data
└── arg=test
With the following as contents of the files in that directory structure:
.. code-block::
# _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:
.. code-block:: python
>>> 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 <https://pypi.org/project/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:
.. code-block:: python
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:
.. code-block:: python
# 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 <http://faux.readthedocs.io/en/latest/index.html>
_.
File an issue in the GitHub issue tracker <https://github.com/bprinty/faux/issues>
_.
FAQs
Quick and easy server mocking.
We found that faux demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.