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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#!/usr/bin/env python
""" Create ScenarioTests from a CLI you've created. """
import os
import sys
import unittest
from collections import OrderedDict
from knack import CLI
from knack.commands import CLICommandsLoader, CommandGroup
from knack.arguments import ArgumentsContext
# DEFINE MY CLI
def a_test_command_handler():
return [{'a': 1, 'b': 1234}, {'a': 3, 'b': 4}]
def abc_list_command_handler():
import string
return list(string.ascii_lowercase)
def hello_command_handler(myarg=None, abc=None):
return ['hello', 'world', myarg, abc]
class MyCommandsLoader(CLICommandsLoader):
def load_command_table(self, args):
with CommandGroup(self, 'hello', '__main__#{}') as g:
g.command('world', 'hello_command_handler', confirmation=True)
with CommandGroup(self, 'abc', '__main__#{}') as g:
g.command('list', 'abc_list_command_handler')
g.command('show', 'a_test_command_handler')
return super(MyCommandsLoader, self).load_command_table(args)
def load_arguments(self, command):
with ArgumentsContext(self, 'hello world') as ac:
ac.argument('myarg', type=int, default=100)
super(MyCommandsLoader, self).load_arguments(command)
name = 'exapp4'
mycli = CLI(cli_name=name,
config_dir=os.path.expanduser(os.path.join('~', '.{}'.format(name))),
config_env_var_prefix=name,
commands_loader_cls=MyCommandsLoader)
# END OF - DEFINE MY CLI
# DEFINE MY TESTS
from knack.testsdk import ScenarioTest, JMESPathCheck
class TestMyScenarios(ScenarioTest):
def __init__(self, method_name):
super(TestMyScenarios, self).__init__(mycli, method_name)
def test_hello_world_yes(self):
self.cmd('hello world --yes', checks=[
JMESPathCheck('length(@)', 4)
])
def test_abc_list(self):
self.cmd('abc list', checks=[
JMESPathCheck('length(@)', 26)
])
def test_abc_show(self):
self.cmd('abc show', checks=[
JMESPathCheck('length(@)', 2),
])
# END OF - DEFINE MY TESTS
if __name__ == '__main__':
unittest.main()
|