I was trying to pass the following test in Travis CI for my click
application:
@pytest.fixture
def runner():
return CliRunner() # from click.testing import CliRunner
enter code here
def test_cli_with_version(runner):
result = runner.invoke(cli.main, ['--version'])
assert result.exit_code == 0
assert not result.exception
assert result.output.strip()
Target function was a standard click
group function.
@click.group(invoke_without_command=True)
@click.help_option(help='Show help index and exit')
@click.version_option(message='%(prog)s v%(version)s', help='Show the version and exit')
@click.option('-p', '--profile', default='dev', help='Set configuration profile')
@click.pass_context
def main(ctx, profile='dev'):
if ctx.invoked_subcommand is None:
click.echo(click.style(title, fg='green'))
ctx.obj = ContextConfig().load_for(profile)
The goal of the test was to simulate a command that looked like main --version
.
When I ran this test in my local environment, it passed (along with the others).
$ pytest
============================ test session starts ============================
platform linux -- Python 3.8.3, pytest-5.4.3, py-1.8.1, pluggy-0.13.1
rootdir: /path/to/root/dir
collected 15 items
tests/test_cli.py .......... [ 66%]
tests/test_contexts.py . [ 73%]
tests/test_models.py . [ 80%]
tests/test_utils.py ... [100%]
============================ 15 passed in 0.30s =============================
However, when Travis tried to build it, this particular test failed. Here's the debug log from Travis
$ pytest
============================= test session starts ==============================
platform linux -- Python 3.6.7, pytest-5.4.3, py-1.8.1, pluggy-0.13.1
rootdir: /path/to/root/dir
collected 15 items
tests/test_cli.py ..F....... [ 66%]
tests/test_contexts.py . [ 73%]
tests/test_models.py . [ 80%]
tests/test_utils.py ... [100%]
=================================== FAILURES ===================================
____________________________ test_cli_with_version _____________________________
runner = <click.testing.CliRunner object at 0x7fe288f596a0>
def test_cli_with_version(runner):
result = runner.invoke(cli.main, ['--version'])
> assert result.exit_code == 0
E AssertionError: assert 1 == 0
E + where 1 = <Result RuntimeError('Could not determine version',)>.exit_code
tests/test_cli.py:35: AssertionError
=========================== short test summary info ============================
FAILED tests/test_cli.py::test_cli_with_version - AssertionError: assert 1 == 0
========================= 1 failed, 14 passed in 0.34s =========================
The command "pytest" exited with 1.
Done. Your build exited with 1.
It looked like click.version_option decorator was failing to read version string in Travis CI's environment. Versioning for the repo was maintained by setuptools-scm
library. However, I have not add any tag to master branch yet.
Here's how .travis.yml
file looks like:
language: python
# target python versions
python:
- "3.6"
- "3.7"
- "3.8"
# operating systems
os:
- linux
- osx
- windows
# configure jobs
jobs:
allow_failures:
- os: windows
- os: osx
# install dependencies
install:
- pip install --upgrade pip
- pip install -r requirements.txt
# run tests
script:
- pytest
# target branches for builds
branches:
only:
- master
How could I fix this?
Turning the comment into an answer:
You need to generate package metadata so the version can be read from. This can be done in different ways, the minimal being
$ python setup.py egg_info
However, much better is installing the package in editable mode:
$ pip install --editable .
For Travis, just add another command to the install
section:
install:
- pip install --upgrade pip
- pip install -r requirements.txt
- pip install --editable .
You can even combine all three commands into one:
- pip install --upgrade pip -r requirements.txt --editable .