Skip to content

Commit

Permalink
tests: call our example entry points via functions
Browse files Browse the repository at this point in the history
I would like to avoid that extra fork to a child Python interpreter (it
looks like something that can be easily avoided). It's something that's
possible now that the code ships just some trivial wrappers (which are,
in turn, needed for setuptools' `console_scripts`).

This cannot use the `capsysbinary` fixture for wrapping of stdout/stderr
due to something in pytest which already got fixed, but has not been
released yet (May 2020). Let's use `capfdbinary` which works fine.

Change-Id: Ic4a124a5cbe2bd24c56e4565d27d313fe4da703f
See-also: pytest-dev/pytest#6926
  • Loading branch information
jktjkt committed Jun 3, 2020
1 parent a2ecfd9 commit 0d12258
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions tests/test_invocation.py
Expand Up @@ -3,24 +3,22 @@
from pathlib import Path
import os
import pytest
import subprocess
from gnpy.tools.cli_examples import transmission_main_example, path_requests_run

SRC_ROOT = Path(__file__).parent.parent


@pytest.mark.parametrize("output, invocation", (
('transmission_main_example',
('./examples/transmission_main_example.py',)),
('path_requests_run',
('./examples/path_requests_run.py',)),
('transmission_main_example__raman',
('./examples/transmission_main_example.py', 'examples/raman_edfa_example_network.json',
'--sim', 'examples/sim_params.json', '--show-channels',)),
@pytest.mark.parametrize("output, handler, args", (
('transmission_main_example', transmission_main_example, []),
('path_requests_run', path_requests_run, []),
('transmission_main_example__raman', transmission_main_example,
['examples/raman_edfa_example_network.json', '--sim', 'examples/sim_params.json', '--show-channels',]),
))
def test_example_invocation(output, invocation):
def test_example_invocation(capfdbinary, output, handler, args):
'''Make sure that our examples produce useful output'''
os.chdir(SRC_ROOT)
expected = open(SRC_ROOT / 'tests' / 'invocation' / output, mode='rb').read()
proc = subprocess.run(invocation, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
assert proc.stderr == b''
assert proc.stdout == expected
handler(args)
captured = capfdbinary.readouterr()
assert captured.out == expected
assert captured.err == b''

0 comments on commit 0d12258

Please sign in to comment.