Skip to main content

A Shell-command faker for Python Unit Testing

Project description

Fakear

A module that creates fake binaries from valid configuration ( a yaml file or a dict ) and launch them in the context of your Python script, without efforts.

Installation

pip install fakear

Quick Start

This is a mock of a fake ls command, that

ls:
  - args:
      - dexter
    return_code: 0
    output: Omelette du Fromage

On Python, you can now use Fakear to fake ls behaviour and act according to our mock.

>>> from subprocess import run
>>> from fakear import Fakear
>>> p = run(["ls", "dexter"])
ls: dexter: No such file or directory
>>> p.returncode
1
>>> with Fakear(cfg="fake_ls.yaml"):
...   fake_cmd = run(["ls", "dexter"])
Omelette du Fromage
>>> fake_cmd.returncode
0

You can use it as well in your unit test routine:

from fakear import Fakear
from subprocess import check_output

def test_faked_ls(self):
    with Fakear(cfg="fake_ls.yaml"):
        proc = check_output(["ls", "dexter"])

    assert proc.decode() == "Omelette du fromage\n"

How does it works ?

An instance handles a specific configuration file, with scenarios for one or multiple commands to fake.

By default, an instance is deactivated, meaning you can still configure it before the fake happens

When activated:

  • The instance creates the folder that should contains the fake binaries (faked_path)
  • It builds shell scripts corresponding to all the scenarios
  • It ensures every scripts are runnable
  • It sets in os.environ["PATH"] the faked_path at first place

So, as long as the instance is activated, you can run faked commands with subprocess module.

When deactivated, the instance removes the faked_path from PATH and deletes the folder from the filesystem

Documentation

YAML Files

Every program mock should start with the program name as a key. Then you can describe multiple behaviours for a given set of arguments.

__command_name__ :
  # Default output with no arguments
  - return_code: -1
    output: This is a fake program, please give the correct arguments
  # output with arguments
  - args:
    - first_arg
    - sec_arg
    return_code: 0
    output: This is an example of fake command
  # output_file with arguments
  - args:
    - first_arg
    - sec_arg
    return_code: 0
    output_file: semver.txt
__command2_name__ :
  # Default behaviour of a program generated by Fakear

You can use those options to customise your fake program:

  • args (Optionna̦l) : a list of positionnal arguments that invoke this fake output
  • return_code : the return code when the program exits
  • output: The raw data to output when you invoke the program with these args
  • output_file: The path of a file containing the output to show

Notice that if you mention no args to your list in a subcommand, it overrides the default behaviour of your fake program.

Also, you have to set either output or output_file keys in the same subcommand. Otherwise, it should throw an error.

API

You can use Fakear in two ways:

  • as an instance:
>>> fakear = Fakear(cfg="path_of_a_valid.yml")
>>> fakear.enable()
# DO THINGS
>>> fakear.disable()

A Fakear instance can be manually enabled or disabled with the correct methods

  • as a Context Manager (recommended):
with Fakear(cfg="fake_ls.yml"):
    # DO THINGS

With the context manager, enable et disable are handled automatically, and you can set the fake path at instanciation

Init variables

Fakear(cfg="/path/cfg.yaml", raw=cfg_data, path="/tmp/bin")
  • cfg: Path of the config file to use
  • raw: Python dict
  • path: Path for fake binaries

You can use either cfg or raw but not both of them, as it should overlap scenarios of the same command and create undefined behaviour.

Available Methods

  • enable() : Activate the instance
  • deactivate() : Deactivate the instance
  • set_path(path: str) : Sets a new path for faked commands. It must be absolute. You can't modify path when the instance is activated

Contribute

Feel free to open an issue on this repository.

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

fakear-0.1.2.tar.gz (7.2 kB view details)

Uploaded Source

Built Distributions

fakear-0.1.2-py3.7.egg (8.6 kB view details)

Uploaded Egg

fakear-0.1.2-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file fakear-0.1.2.tar.gz.

File metadata

  • Download URL: fakear-0.1.2.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for fakear-0.1.2.tar.gz
Algorithm Hash digest
SHA256 84305e83dae729667ee400871000c51220fe7e38fb2383960de51851d542061f
MD5 89f2fc16d3c33a60ab49350f6e292f34
BLAKE2b-256 0c8886b6f0b744309510141ae6bc91c3df081617112fda178fe1341f3b477dd8

See more details on using hashes here.

File details

Details for the file fakear-0.1.2-py3.7.egg.

File metadata

  • Download URL: fakear-0.1.2-py3.7.egg
  • Upload date:
  • Size: 8.6 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for fakear-0.1.2-py3.7.egg
Algorithm Hash digest
SHA256 58aef436bc2e2acc4470fb579f7e3064048cdafd5de2f1e48e71ba93c3cf1284
MD5 e01982b579525197f799cb1da190439f
BLAKE2b-256 ba5b06e3246afac45f58dc3f4d1173c574215433aeee810043c6a3d2af04f248

See more details on using hashes here.

File details

Details for the file fakear-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: fakear-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for fakear-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e9b4e73dd5a93ebba46836124f0606b46ed2d4d54ab6df53f21f21d1b79e4070
MD5 15f459a6b61ee47a14a176ff6d1fe15c
BLAKE2b-256 9955bbb93c3bd0f826638dee915e1e7da7717109d1874c8651c8341530714b9c

See more details on using hashes here.

Supported by

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