summaryrefslogtreecommitdiff
path: root/plugins/ext_test/tests/test_ext_test.py
blob: b1ba1b7d47b8d1ed43430a2e5175d6a2fefcf010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#
# coding=utf-8

import pytest

import cmd2_ext_test
from cmd2 import CommandResult, cmd2

######
#
# 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