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-fork-0.1.3.dev1.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fakear_fork-0.1.3.dev1-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file fakear-fork-0.1.3.dev1.tar.gz.

File metadata

  • Download URL: fakear-fork-0.1.3.dev1.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.3

File hashes

Hashes for fakear-fork-0.1.3.dev1.tar.gz
Algorithm Hash digest
SHA256 eb10595c03f106855793f24e43eb95f33dab15ae4e37fbf5274b2ad977f7762f
MD5 3c6ebbe4c9858b860d79673c5090417e
BLAKE2b-256 58c07ce9c6e7a5da6f9d670213bf9fcf1af062bda79084c9c9cfde06cd2e7ba0

See more details on using hashes here.

File details

Details for the file fakear_fork-0.1.3.dev1-py3-none-any.whl.

File metadata

  • Download URL: fakear_fork-0.1.3.dev1-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.3

File hashes

Hashes for fakear_fork-0.1.3.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 a8ddd0a56622e25d7ccff2c47693bb8c016e19144eb95ddf950eb6d92337cc62
MD5 15bc35199b18a05775ae5330e7d39d38
BLAKE2b-256 409cc39217571fe36cecd3e60f150b56450437b1a0f3f5af04ce79fd91283a5e

See more details on using hashes here.

Supported by

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