Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test_exapp #288

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions knack/testsdk/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@


class IntegrationTestBase(unittest.TestCase):
def __init__(self, cli, method_name):
def __init__(self, method_name):
super().__init__(method_name)
self.cli = cli
self.cli = None
self.diagnose = os.environ.get(ENV_TEST_DIAGNOSE, None) == 'True'

def cmd(self, command, checks=None, expect_failure=False):
Expand Down Expand Up @@ -81,8 +81,8 @@ class LiveTest(IntegrationTestBase):

class ScenarioTest(IntegrationTestBase): # pylint: disable=too-many-instance-attributes

def __init__(self, cli, method_name, filter_headers=None):
super().__init__(cli, method_name)
def __init__(self, method_name, filter_headers=None):
super().__init__(method_name)
Comment on lines +84 to +85
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#287 (comment) explains why positional argument cli is removed.

self.name_replacer = GeneralNameReplacer()
self.recording_processors = [LargeRequestBodyProcessor(),
LargeResponseBodyProcessor(),
Expand Down
9 changes: 5 additions & 4 deletions examples/test_exapp → tests/test_exapp.py
100755 → 100644
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .py file extension is necessary, otherwise pytest won't collect this file at all.

> pytest examples\test_exapp
================================================= test session starts =================================================
platform win32 -- Python 3.11.9, pytest-8.2.2, pluggy-1.5.0
rootdir: D:\cli\knack
plugins: forked-1.6.0, xdist-3.6.1
collected 0 items

================================================ no tests ran in 0.02s ================================================
ERROR: not found: D:\cli\knack\examples\test_exapp
(no match in any of [<Dir examples>])

That's why the error is not exposed in CI.

Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def hello_command_handler(myarg=None, abc=None):
class MyCommandsLoader(CLICommandsLoader):

def load_command_table(self, args):
with CommandGroup(self, 'hello', '__main__#{}') as g:
with CommandGroup(self, 'hello', '{}#{{}}'.format(__name__)) as g:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__main__ denotes this file only when this file is directly invoked by python.

When invoked from pytest, __main__ is actually D:\cli\py311\Scripts\pytest.exe\__main__.py. This can be verified by adding import __main__; print(__main__):

> pytest examples\test_exapp.py -s
================================================= test session starts =================================================
platform win32 -- Python 3.11.9, pytest-8.2.2, pluggy-1.5.0
rootdir: D:\cli\knack
plugins: forked-1.6.0, xdist-3.6.1
collecting ... <module '__main__' from 'D:\\cli\\py311\\Scripts\\pytest.exe\\__main__.py'>

g.command('world', 'hello_command_handler', confirmation=True)
with CommandGroup(self, 'abc', '__main__#{}') as g:
with CommandGroup(self, 'abc', '{}#{{}}'.format(__name__)) as g:
g.command('list', 'abc_list_command_handler')
g.command('show', 'a_test_command_handler')
return super(MyCommandsLoader, self).load_command_table(args)
Expand Down Expand Up @@ -61,8 +61,9 @@ def load_arguments(self, command):

class TestMyScenarios(ScenarioTest):

def __init__(self, method_name):
super(TestMyScenarios, self).__init__(mycli, method_name)
def setUp(self):
super().setUp()
self.cli = mycli
Comment on lines +64 to +66
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initializing self.cli in setUp adheres to the "do heavy work in setUp" principle: Azure/azure-cli#28849


def test_hello_world_yes(self):
self.cmd('hello world --yes', checks=[
Expand Down
Loading