pytest: use subtests for various layout options, add coverage
parent
91a340f619
commit
b03116f4a6
@ -1,2 +1,4 @@
|
||||
pytest
|
||||
pytest-subtests
|
||||
coverage
|
||||
black
|
||||
|
@ -1,54 +0,0 @@
|
||||
# pylint: disable=unused-import
|
||||
|
||||
from os.path import dirname, realpath
|
||||
from os import chdir
|
||||
import sys
|
||||
import logging
|
||||
import io
|
||||
import collections
|
||||
from unittest import TestCase as VanillaTestCase, skip, main as unittests_main
|
||||
import pytest
|
||||
from ocrd_utils import disableLogging, initLogging
|
||||
|
||||
def main(fn=None):
|
||||
if fn:
|
||||
sys.exit(pytest.main([fn]))
|
||||
else:
|
||||
unittests_main()
|
||||
|
||||
class TestCase(VanillaTestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
chdir(dirname(realpath(__file__)) + '/..')
|
||||
|
||||
def setUp(self):
|
||||
disableLogging()
|
||||
initLogging()
|
||||
|
||||
class CapturingTestCase(TestCase):
|
||||
"""
|
||||
A TestCase that needs to capture stderr/stdout and invoke click CLI.
|
||||
"""
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _setup_pytest_capfd(self, capfd):
|
||||
self.capfd = capfd
|
||||
|
||||
def invoke_cli(self, cli, args):
|
||||
"""
|
||||
Substitution for click.CliRunner.invooke that works together nicely
|
||||
with unittests/pytest capturing stdout/stderr.
|
||||
"""
|
||||
self.capture_out_err() # XXX snapshot just before executing the CLI
|
||||
code = 0
|
||||
sys.argv[1:] = args # XXX necessary because sys.argv reflects pytest args not cli args
|
||||
try:
|
||||
cli.main(args=args)
|
||||
except SystemExit as e:
|
||||
code = e.code
|
||||
out, err = self.capture_out_err()
|
||||
return code, out, err
|
||||
|
||||
def capture_out_err(self):
|
||||
return self.capfd.readouterr()
|
@ -1,24 +1,39 @@
|
||||
from os import environ
|
||||
from pathlib import Path
|
||||
from ocrd_utils import pushd_popd
|
||||
from tests.base import CapturingTestCase as TestCase, main
|
||||
from eynollah.cli import layout as eynollah_cli
|
||||
from click.testing import CliRunner
|
||||
|
||||
testdir = Path(__file__).parent.resolve()
|
||||
|
||||
EYNOLLAH_MODELS = environ.get('EYNOLLAH_MODELS', str(testdir.joinpath('..', 'models_eynollah').resolve()))
|
||||
|
||||
class TestEynollahRun(TestCase):
|
||||
|
||||
def test_full_run(self):
|
||||
with pushd_popd(tempdir=True) as tempdir:
|
||||
code, out, err = self.invoke_cli(eynollah_cli, [
|
||||
'-m', EYNOLLAH_MODELS,
|
||||
'-i', str(testdir.joinpath('resources/kant_aufklaerung_1784_0020.tif')),
|
||||
'-o', tempdir
|
||||
])
|
||||
print(code, out, err)
|
||||
assert not code
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(__file__)
|
||||
def test_full_run(tmpdir, subtests, pytestconfig):
|
||||
args = [
|
||||
'-m', EYNOLLAH_MODELS,
|
||||
'-i', str(testdir.joinpath('resources/kant_aufklaerung_1784_0020.tif')),
|
||||
'-o', tmpdir,
|
||||
# subtests write to same location
|
||||
'--overwrite',
|
||||
]
|
||||
if pytestconfig.getoption('verbose') > 0:
|
||||
args.extend(['-l', 'DEBUG'])
|
||||
runner = CliRunner()
|
||||
for options in [
|
||||
[], # defaults
|
||||
["--allow_scaling", "--curved-line"],
|
||||
["--allow_scaling", "--curved-line", "--full-layout"],
|
||||
["--allow_scaling", "--curved-line", "--full-layout", "--reading_order_machine_based"],
|
||||
["--allow_scaling", "--curved-line", "--full-layout", "--reading_order_machine_based",
|
||||
"--textline_light", "--light_version"],
|
||||
# -ep ...
|
||||
# -eoi ...
|
||||
# --do_ocr
|
||||
# --skip_layout_and_reading_order
|
||||
]:
|
||||
with subtests.test(#msg="test CLI",
|
||||
options=options):
|
||||
result = runner.invoke(eynollah_cli, args + options)
|
||||
print(result)
|
||||
print(result.output)
|
||||
assert result.exit_code == 0
|
||||
assert 'kant_aufklaerung_1784_0020.tif' in result.output
|
||||
|
Loading…
Reference in New Issue