| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
""" Create ScenarioTests from a CLI you've created. """ |
| 4 |
|
| 5 |
import os |
| 6 |
import sys |
| 7 |
import unittest |
| 8 |
|
| 9 |
from collections import OrderedDict |
| 10 |
|
| 11 |
from knack import CLI |
| 12 |
from knack.commands import CLICommandsLoader, CommandGroup |
| 13 |
from knack.arguments import ArgumentsContext |
| 14 |
|
| 15 |
# DEFINE MY CLI |
| 16 |
|
| 17 |
|
| 18 |
def a_test_command_handler(): |
| 19 |
return [{'a': 1, 'b': 1234}, {'a': 3, 'b': 4}] |
| 20 |
|
| 21 |
|
| 22 |
def abc_list_command_handler(): |
| 23 |
import string |
| 24 |
return list(string.ascii_lowercase) |
| 25 |
|
| 26 |
|
| 27 |
def hello_command_handler(myarg=None, abc=None): |
| 28 |
return ['hello', 'world', myarg, abc] |
| 29 |
|
| 30 |
|
| 31 |
class MyCommandsLoader(CLICommandsLoader): |
| 32 |
|
| 33 |
def load_command_table(self, args): |
| 34 |
with CommandGroup(self, 'hello', '__main__#{}') as g: |
| 35 |
g.command('world', 'hello_command_handler', confirmation=True) |
| 36 |
with CommandGroup(self, 'abc', '__main__#{}') as g: |
| 37 |
g.command('list', 'abc_list_command_handler') |
| 38 |
g.command('show', 'a_test_command_handler') |
| 39 |
return super(MyCommandsLoader, self).load_command_table(args) |
| 40 |
|
| 41 |
def load_arguments(self, command): |
| 42 |
with ArgumentsContext(self, 'hello world') as ac: |
| 43 |
ac.argument('myarg', type=int, default=100) |
| 44 |
super(MyCommandsLoader, self).load_arguments(command) |
| 45 |
|
| 46 |
|
| 47 |
name = 'exapp4' |
| 48 |
|
| 49 |
mycli = CLI(cli_name=name, |
| 50 |
config_dir=os.path.expanduser(os.path.join('~', '.{}'.format(name))), |
| 51 |
config_env_var_prefix=name, |
| 52 |
commands_loader_cls=MyCommandsLoader) |
| 53 |
|
| 54 |
|
| 55 |
# END OF - DEFINE MY CLI |
| 56 |
|
| 57 |
# DEFINE MY TESTS |
| 58 |
|
| 59 |
from knack.testsdk import ScenarioTest, JMESPathCheck |
| 60 |
|
| 61 |
|
| 62 |
class TestMyScenarios(ScenarioTest): |
| 63 |
|
| 64 |
def __init__(self, method_name): |
| 65 |
super(TestMyScenarios, self).__init__(mycli, method_name) |
| 66 |
|
| 67 |
def test_hello_world_yes(self): |
| 68 |
self.cmd('hello world --yes', checks=[ |
| 69 |
JMESPathCheck('length(@)', 4) |
| 70 |
]) |
| 71 |
|
| 72 |
def test_abc_list(self): |
| 73 |
self.cmd('abc list', checks=[ |
| 74 |
JMESPathCheck('length(@)', 26) |
| 75 |
]) |
| 76 |
|
| 77 |
def test_abc_show(self): |
| 78 |
self.cmd('abc show', checks=[ |
| 79 |
JMESPathCheck('length(@)', 2), |
| 80 |
]) |
| 81 |
|
| 82 |
# END OF - DEFINE MY TESTS |
| 83 |
|
| 84 |
|
| 85 |
if __name__ == '__main__': |
| 86 |
unittest.main() |