Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bob.io.base
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
bob
bob.io.base
Commits
7802ee67
Commit
7802ee67
authored
2 years ago
by
Yannick DAYER
Browse files
Options
Downloads
Patches
Plain Diff
Add a click command result handler for tests.
parent
74771fea
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!44
Add click result utility
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
bob/io/base/test/test_utlilities.py
+38
-0
38 additions, 0 deletions
bob/io/base/test/test_utlilities.py
bob/io/base/test_utils.py
+40
-0
40 additions, 0 deletions
bob/io/base/test_utils.py
with
78 additions
and
0 deletions
bob/io/base/test/test_utlilities.py
0 → 100644
+
38
−
0
View file @
7802ee67
import
sys
import
click
from
click.testing
import
CliRunner
from
bob.io.base
import
test_utils
@click.command
(
"
dummy
"
)
def
dummy_command_0
():
sys
.
exit
(
0
)
@click.command
(
"
dummy_exit_1
"
)
def
dummy_command_1
():
sys
.
exit
(
1
)
@click.command
(
"
dummy_exit_raise
"
)
def
dummy_command_raise
():
raise
RuntimeError
(
"
Expected exception
"
)
def
test_assert_dummy
():
result
=
CliRunner
().
invoke
(
dummy_command_0
)
assert
result
.
exit_code
==
0
test_utils
.
assert_click_runner_result
(
result
)
result
=
CliRunner
().
invoke
(
dummy_command_1
)
assert
result
.
exit_code
==
1
test_utils
.
assert_click_runner_result
(
result
,
exit_code
=
1
)
result
=
CliRunner
().
invoke
(
dummy_command_raise
)
assert
result
.
exit_code
==
1
test_utils
.
assert_click_runner_result
(
result
,
exit_code
=
1
,
exception_type
=
RuntimeError
)
This diff is collapsed.
Click to expand it.
bob/io/base/test_utils.py
+
40
−
0
View file @
7802ee67
...
@@ -10,8 +10,13 @@
...
@@ -10,8 +10,13 @@
import
functools
import
functools
import
os
import
os
import
traceback
import
unittest
import
unittest
from
typing
import
Optional
import
click.testing
def
datafile
(
f
,
module
=
None
,
path
=
"
data
"
):
def
datafile
(
f
,
module
=
None
,
path
=
"
data
"
):
"""
datafile(f, [module], [data]) -> filename
"""
datafile(f, [module], [data]) -> filename
...
@@ -117,3 +122,38 @@ def extension_available(extension):
...
@@ -117,3 +122,38 @@ def extension_available(extension):
return
wrapper
return
wrapper
return
test_wrapper
return
test_wrapper
def
assert_click_runner_result
(
result
:
click
.
testing
.
Result
,
exit_code
:
int
=
0
,
exception_type
:
Optional
[
Exception
]
=
None
,
):
"""
Helper for asserting click runner results.
Parameters
----------
result
The return value on ``click.testing.CLIRunner.invoke()``.
exit_code
The expected command exit code (defaults to 0).
exception_type
If given, will ensure that the raised exception is of that type.
"""
m
=
(
"
Click command exited with code
'
{}
'
, instead of
'
{}
'
.
\n
"
"
Exception:
\n
{}
\n
"
"
Output:
\n
{}
"
)
exception
=
(
"
None
"
if
result
.
exc_info
is
None
else
""
.
join
(
traceback
.
format_exception
(
*
result
.
exc_info
))
)
m
=
m
.
format
(
result
.
exit_code
,
exit_code
,
exception
,
result
.
output
)
assert
result
.
exit_code
==
exit_code
,
m
if
exit_code
==
0
:
assert
not
result
.
exception
,
m
if
exception_type
is
not
None
:
assert
isinstance
(
result
.
exception
,
exception_type
),
m
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment