Skip to main content

Mock command line utils by replaying recorded invocations

Project description

See Build Status on Travis CI

This package provides two command line utilities: crecord and creplay. The former records the output (stdout and stderr) and the return code of a command and the latter replays the command invocation by reproducing its output and return code:

$ crecord echo foo
foo
$ creplay echo foo
foo
$ crecord ls foo
ls: foo: No such file or directory
$ creplay ls foo
ls: foo: No such file or directory
$ echo $?
1

This could be used to mock slow and environment-dependent command invocations for testing purposes.

Pytest plugin

There’s also a pytest plugin contained in cli_mock package. It exposes popen_controller fixture that can be used to replay crecord logs in response to subprocess.Popen invocations (and the APIs that call it under the hood):

def test_foo(popen_controller):
    popen_controller.set_replay_log(my_log)
    output = subprocess.check_output(['foo'])
    assert output == b'bar\n'

After the replay log is activated calls to subprocess.Popen and friends with the commands that are contained in the log will replay from the log. Commands that are not in the log will trigger an AssertionError.

Non-strict mode

It’s possible to allow executing commands that are not in the log via activating non-strict mode:

popen_controller.set_strict(False)

or:

popen_controller.set_replay_log(my_log, strict=False)

In non-strict mode any command that is not in the log will be passed to subprocess.Popen without modifications and will be executed in a usual fashion.

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

cli_mock-0.2.1.tar.gz (4.6 kB view details)

Uploaded Source

File details

Details for the file cli_mock-0.2.1.tar.gz.

File metadata

  • Download URL: cli_mock-0.2.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for cli_mock-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c4ba87df89d33d1dda4f0bb5b333e41145cf059a273671a904994cf43afe7dae
MD5 84f17557616a46c965b2948ca44ddef2
BLAKE2b-256 1f119cbb545a26674adb8c69dcef10c056873b43cfdda367838f3c20fd317ad0

See more details on using hashes here.

Supported by

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