hbutils.testing.simulate

Overview:

Simulation for some special behaviour in Python.

simulate_entry

hbutils.testing.simulate.simulate_entry(entry: Callable, argv: Optional[List[str]] = None, envs: Optional[Mapping[str, str]] = None) → hbutils.testing.simulate.entry.EntryRunResult[source]
Overview:

CLI entry’s simulation.

Parameters:
  • entry – Entry function, should be a simple function without any arguments.

  • argv – Command line arguments. Default is None, which means do not mock sys.argv.

  • envs – Environment arguments. Default is None, which means do not mock os.environ.

Returns:

A result object, in form of EntryRunResult.

Examples::

We create a simple CLI code with click package, named test_cli1.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
 import sys
 import click

 @click.command('cli1', help='CLI-1 example')
 @click.option('-c', type=int, help='optional C value', default=None)
 @click.argument('a', type=int)
 @click.argument('b', type=int)
 def cli1(a, b, c):
     if c is None:
         print(f'{a} + {b} = {a + b}')
     else:
         print(f'{a} + {b} + {c} = {a + b + c}', file=sys.stderr)

 if __name__ == '__main__':
     cli1()

When we can try to simulate it.

>>> from hbutils.testing import simulate_entry
>>> from test_cli1 import cli1
>>> r1 = simulate_entry(cli1, ['cli1', '2', '3'])
>>> print(r1.exitcode)
0
>>> print(r1.stdout)
2 + 3 = 5
>>> r2 = simulate_entry(cli1, ['cli1', '2', '3', '-c', '24'])  # option
>>> print(r2.exitcode)
0
>>> print(r2.stderr)
2 + 3 + 24 = 29
>>> r3 = simulate_entry(cli1, ['cli', '--help'])  # help
>>> print(r3.stdout)
Usage: cli [OPTIONS] A B
  CLI-1 example
Options:
  -c INTEGER  optional C value
  --help      Show this message and exit.
>>> r4 = simulate_entry(cli1, ['cli', 'dklsfj'])  # misusage
>>> print(r4.exitcode)
2
>>> print(r4.stderr)
Usage: cli [OPTIONS] A B
Try 'cli --help' for help.
Error: Invalid value for 'A': 'dklsfj' is not a valid integer.

Note

Please note that if there is uncaught exception raised inside the entry function, it will be caught and put into error property instead of be being printed to stderr. For example

>>> from hbutils.testing import simulate_entry
>>> def my_cli():
...     raise ValueError(233)
>>>
>>> r = simulate_entry(my_cli)
>>> print(r.exitcode)  # will be 0x1
1
>>> print(r.stdout)  # nothing
>>> print(r.stderr)  # nothing as well
>>> print(repr(r.error))  # HERE!!!
ValueError(233)

EntryRunResult

class hbutils.testing.simulate.EntryRunResult(exitcode: int, stdout: Optional[str], stderr: Optional[str], error: Optional[BaseException])[source]
Overview:

Run result of one entry.

__init__(exitcode: int, stdout: Optional[str], stderr: Optional[str], error: Optional[BaseException])[source]

Constructor of EntryRunResult.

Parameters:
  • exitcode – Exit code.

  • stdout – Output in standard output stream.

  • stderr – Output in standard error stream.

  • error – Uncaught exception raised inside.

property error

Uncaught exception raised inside.

property exitcode

Exit code.

property stderr

Output in standard error stream.

property stdout

Output in standard output stream.