Testing applications for python

click is a python package for creating nice command line interfaces for your applications. I played with a click a little and today clicked this simple roman digital converter on github.

Now I want to test the test application. I read the documentation, but don’t know how to run the tests.

Does anyone have experience testing click apps?

+5
source share
2 answers

Entering the code below in test_greet.py :

 import click from click.testing import CliRunner def test_greet(): @click.command() @click.argument('name') def greet(name): click.echo('Hello %s' % name) runner = CliRunner() result = runner.invoke(greet, ['Sam']) assert result.output == 'Hello Sam\n' if __name__ == '__main__': test_greet() 

If just called with python test_greet.py , the tests pass and nothing is displayed. When used as part of a test, it works as expected. For example, nosetests test_greet.py returns

 . ---------------------------------------------------------------------- Ran 1 test in 0.002s OK 
+3
source

pytest has handlers for assertions.

To run tests with an existing script, it must be "imported".

 import click from click.testing import CliRunner from click_app import configure, cli def test_command_configure(): runner = CliRunner() result = runner.invoke(cli, ["configure"]) assert result.exit_code == 0 assert result.output == 'configure' 
0
source

Source: https://habr.com/ru/post/1206287/


All Articles