Skip to main content

Serverboards Mock Library -- SMock

SMock is a simple mocking library that helps automated testing of Python code.

Basic usage is to create a yaml file with the mocked functions as keys, and a list of marching args list/kwargs object to a result.

All possible matches are checked in order, and if any matches, that response is given to the caller. A special * marker can be used to match any.

Example of use

Generate a yaml file with the data to mock, mock.yaml:

requests.get:
  - args: https://example.com/item/1
  - result:
    status_code: 200
    json:
      desription: Mocked
requests.get:
  - args: "*"
    result:
      status_code: 404
      content: Not found

This file says that if the user asks for the https://mock.example, it gets a status_code 200 and more info. Any other request, gets a 404.

To use it:

import requests

def get_item_description(id):
  data = requests.get("https://example.com/item/%s" % id)
  return data.json()["description"]

def test():
  smock import SMock
  smock = SMock("data.yaml")
  requests.get = smock.mock_method("requests.get")

  assert get_item_description(1) == "Mocked"

if __name__ == "__main__":
  test()

Tis hypotetical library, has a function get_item_description. When calling this file as a script (__main__), the test is called which monkey patches the requests.get function with our mocked data.

This mocked data returns a special object that behaves like a dict, an object a callable function and so on. In this case we call the json() virtual method to just get an object with a description field.

Extra goodies

  • All data is mocked to behave like a dict, a object and a function
  • Can load external data with file:FILENAME

Contributing

If you have ideas, improvements or comments, please open a github issue.

Metadata

Release files for smock 0.2.13

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

Source distribution (sdist)

Source distribution for smock 0.2.13
File Size Uploaded Provenance Actions
smock-0.2.13.tar.gz 4.9 kB Without provenance Details for smock-0.2.13.tar.gz

Release files / smock-0.2.13.tar.gz

Download URL smock-0.2.13.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1a05aae74d3757843ad20009189efb98f0bcfe67828496d5348025c550418f6d
BLAKE2b-256 checksum
How to use checksums
27235ef9917fc3a1478bbc7b888db8887c185069d872c4ad3b2d1ce676e3ab66
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.7.5

Security for smock 0.2.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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