Package: cli-result
hide:navigationCli_resultsSimple lib to test results or script runs from command line.InstallInstall from pypi:pip install cli_resultOr install from github repo:pip install git+https://github.com/ayasyrev/cli_result.gitUsage.Main purpose test results of examples run. We run all scripts in examples folder and compare results with expected results. Check it at different python versions.So we can be sure that all scripts work and has similar behaviors in different python versions.It's not suitable to run script that supposed to run for a long time or resources are limited.But it's good for quick tests, to check configs and shorts demos (examples).Put your script in examples folder and expected results in results folder.Arguments for tests at file name same as script name +__args.txt.fromcli_resultimportcheck_examples,Cfgerrors=check_examples()This run all scripts in examples folder with arguments from__args.txtfile and compare with results atresults/folder.asserterrorsisNoneExamplesWe can change examples folder.cfg=Cfg(examples_path="../examples/examples_extra/")Check examples at folder:fromcli_resultimportget_examplesexamples=get_examples(cfg=cfg)We got list of examples as named tuple example_name, filesexample=examples[0]# nameprint(example.name)# or example[0]# filesprint(example.files[0])print(example[1][1])outputexample_extra_1
    ../examples/examples_extra/example_extra_1.py
    ../examples/examples_extra/example_extra_1__alter.py</pre>Run scriptWe can run script and look at result.fromcli_resultimportrun_scriptresult=run_script(filename=example[1][0],args="--help",)print(result.stdout)outputusage: example_extra_1.py [-h] [--echo ECHO]options:
  -h, --help   show this help message and exit
  --echo ECHO


</pre>assertresult.stderr==""Load expected result.fromcli_resultimportread_result,get_argsLoad arguments for example.get_argsreturns list ofArgsargs=get_args(example.name,cfg)print(args[0])outputArgs(name='help', list=['--help'])</pre>expected=read_result(name=example.name,arg_name=args[0].name,# or args[0][0]cfg=cfg,)Now we can compare results.assertresult==expectedCheck one example.We can check one example.fromcli_resultimportrun_check_exampleerrors=run_check_example(example_name=example.name,file_list=example.files,cfg=cfg,)asserterrorsisNoneAlternatively we can check one as:errors=check_examples(names=example.name,# we can send list of names as [name1, name2, ...]cfg=cfg,)asserterrorsisNoneCheck all examples.Or check all examples.errors=check_examples(cfg=cfg)asserterrorsisNoneCheck errorsLets look at example with error.cfg=Cfg(examples_path="../tests/examples/examples_errors/")errors=check_examples(cfg=cfg)asserterrorsisnotNoneprint(f"Examples with errors:{len(errors)},{examples[0].name}:{len(errors[0].list)}errors")outputExamples with errors: 1, example_extra_1: 10 errors</pre>Let look at one of errors.We got file name that has error, result of run and expected result. Now we can look for what is wrong.example_error=errors[0]print(example_error.name)outputexmpl_1</pre>error=example_error.list[4]print(error.argname)print(error.filename)outputempty_str
    ../tests/examples/examples_errors/exmpl_1__err_1.py</pre>We can compare result with expected result.print(error.res)outputusage: exmpl_1__err_1.py [-h] [--e E]
    exmpl_1__err_1.py: error: unrecognized arguments: ""</pre>And expected is:print(error.exp)outputusage: exmpl_1.py [-h] [--echo ECHO]
    exmpl_1.py: error: unrecognized arguments: ""</pre>