0

I've built a python command line application, which operates in two modes:

  1. in an interactive mode, where it opens up an interactive shell using python CMDModule.

    Example:

    running plcli in command line, opens up its interactive mode

    (cmd)> do_x

  2. reads commands from an input file and executes them one-by-one.

    Example:

    running plcli input_file.txt in command line, it executes commands line-by-line and outputs result to stdout.

How do I write unit and integration test for this cli app. I've explored pytest-console-scripts and script-test but they doesn't seem to be optimum or best practice.

Please suggest accepted/best practise libraries or usage to test python command line applications.

Note : I'm not using any python cli frameworks like clint or click

1
  • In what way does the command-line nature of your program influence testing? Write tests for the classes and functions you have. Commented May 20, 2018 at 17:54

1 Answer 1

1

The most important principle is to organize your code so that the complexity resides in functions (or methods) that are amenable to straightforward unit testing -- in other words, in functions that don't deal with the command-line or interactive nature of the larger program. Then glue those functions to a thin outer layer that provides the command-line or interactive functionality. That glue will tend to be very simple and might not be worth the trouble of testing. If it does need any testing, you can often do it with a small number of basic end-to-end executions (e.g. for the command-line scenario a test that uses Python's subprocess module to run your program exactly the way a user would).

More details can be found in Gary Bernhardt's excellent Boundaries talk.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.