From 601a8e271020ca1b0918deabf70ad0778ded7d4a Mon Sep 17 00:00:00 2001 From: Eric Lin Date: Tue, 7 Jul 2020 15:57:02 -0400 Subject: external test plugin tests and coverage should now run --- plugins/ext_test/tests/__init__.py | 2 + plugins/ext_test/tests/pylintrc | 19 +++++++++ plugins/ext_test/tests/test_ext_test.py | 70 +++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 plugins/ext_test/tests/__init__.py create mode 100644 plugins/ext_test/tests/pylintrc create mode 100644 plugins/ext_test/tests/test_ext_test.py (limited to 'plugins/ext_test/tests') diff --git a/plugins/ext_test/tests/__init__.py b/plugins/ext_test/tests/__init__.py new file mode 100644 index 00000000..eb198dc0 --- /dev/null +++ b/plugins/ext_test/tests/__init__.py @@ -0,0 +1,2 @@ +# +# empty file to create a package diff --git a/plugins/ext_test/tests/pylintrc b/plugins/ext_test/tests/pylintrc new file mode 100644 index 00000000..1dd17c1c --- /dev/null +++ b/plugins/ext_test/tests/pylintrc @@ -0,0 +1,19 @@ +# +# pylint configuration for tests package +# +# $ pylint --rcfile=tests/pylintrc tests +# + +[basic] +# allow for longer method and function names +method-rgx=(([a-z][a-z0-9_]{2,50})|(_[a-z0-9_]*))$ +function-rgx=(([a-z][a-z0-9_]{2,50})|(_[a-z0-9_]*))$ + +[messages control] +# too-many-public-methods -> test classes can have lots of methods, so let's ignore those +# missing-docstring -> prefer method names instead of docstrings +# no-self-use -> test methods part of a class hardly ever use self +# unused-variable -> sometimes we are expecting exceptions +# redefined-outer-name -> pylint fixtures cause these +# protected-access -> we want to test private methods +disable=too-many-public-methods,missing-docstring,no-self-use,unused-variable,redefined-outer-name,protected-access diff --git a/plugins/ext_test/tests/test_ext_test.py b/plugins/ext_test/tests/test_ext_test.py new file mode 100644 index 00000000..cf5429b8 --- /dev/null +++ b/plugins/ext_test/tests/test_ext_test.py @@ -0,0 +1,70 @@ +# +# coding=utf-8 + +import pytest + +from cmd2 import cmd2, CommandResult +import cmd2_ext_test + +###### +# +# define a class which implements a simple cmd2 application +# +###### + +OUT_MSG = 'this is the something command' + + +class ExampleApp(cmd2.Cmd): + """An class to show how to use a plugin""" + def __init__(self, *args, **kwargs): + # gotta have this or neither the plugin or cmd2 will initialize + super().__init__(*args, **kwargs) + + def do_something(self, _): + self.last_result = 5 + self.poutput(OUT_MSG) + + +# Define a tester class that brings in the external test mixin + +class ExampleTester(cmd2_ext_test.ExternalTestMixin, ExampleApp): + def __init__(self, *args, **kwargs): + # gotta have this or neither the plugin or cmd2 will initialize + super().__init__(*args, **kwargs) + +# +# You can't use a fixture to instantiate your app if you want to use +# to use the capsys fixture to capture the output. cmd2.Cmd sets +# internal variables to sys.stdout and sys.stderr on initialization +# and then uses those internal variables instead of sys.stdout. It does +# this so you can redirect output from within the app. The capsys fixture +# can't capture the output properly in this scenario. +# +# If you have extensive initialization needs, create a function +# to initialize your cmd2 application. + + +@pytest.fixture +def example_app(): + app = ExampleTester() + app.fixture_setup() + yield app + app.fixture_teardown() + + +##### +# +# unit tests +# +##### + +def test_something(example_app): + # load our fixture + # execute a command + out = example_app.app_cmd("something") + + # validate the command output and result data + assert isinstance(out, CommandResult) + assert str(out.stdout).strip() == OUT_MSG + assert out.data == 5 -- cgit v1.2.1