painless-software / python-cli-test-helpers

Useful helpers for writing tests for your Python CLI program.
https://python-cli-test-helpers.readthedocs.io
GNU General Public License v3.0
30 stars 4 forks source link

'test_example_command' does not seem to falsify as expected. #25

Closed elizaaverywilson closed 1 year ago

elizaaverywilson commented 1 year ago

In the example CLI for Click, there is the following test:

def test_example_command():
    """
    Is command available?
    """
    result = shell('foobar example --help')
    assert result.exit_code == 0

My understanding of the purpose of this test is to show that the command 'example' is accessible. So, I would expect it to be falsified by having a command that is not part of foobar's CLI. I've made this test to demonstrate:

def test_example_command_bug():
    """
    Is command available?
    """
    result = shell('foobar nonexistant-command --help')
    assert result.exit_code != 0

However, when I run this test, result.exit_code is 0. The test fails. Am I understanding the purpose of test_example_command() correctly? I am trying to use it in my application's tests, but it passes when I don't expect it to.

elizaaverywilson commented 1 year ago

You can view a fork of the repository with the added test here.

bittner commented 1 year ago

Hi @elizaaverywilson, thank you for your interest in this project!

I took a look at the problem: The code behaves alright, the function name of the test might be confusing, though. The CLI parameter isn't a "command", but really an arbitrary text argument.

Hence, "nonexistant-command" is actually a valid argument, and your test proves it. You can convince yourself by running the code for real and playing with the CLI, e.g.

$ pipenv shell
...
$ pip install .
...
$ foobar nonexistant-command --help
Usage: foobar [OPTIONS] ENVVAR

  Click CLI Example

Options:
  --version  Show the version and exit.
  --help     Show this message and exit.
$ foobar nonexistant-command
Environment value nonexistant-command not set.

I agree that the CLI example can be improved. The code made more sense in its original context where I took it from. After reducing the code example what's left is confusion. I apologize! :roll_eyes:

The Docopt example is more obvious, I believe. I had planned to align the 3 CLI framework examples. If you have an immediate idea of how to improve the Click example feel free to create a pull request.

Thanks again for letting me know about the issue! :100: :medal_sports:

bittner commented 1 year ago

Closing this issue as clarified. Feel free to comment or reopen! :rocket: