-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#15 Skeleton and parser for apply command
+ mock_envs decorator can now be applied to test classes + mock_envs decorator was moved to a separate module + test for apply command parser and main function + test_main now uses mock_envs decorator also
- Loading branch information
Showing
6 changed files
with
71 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# ToDo: add tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Utilities for tests.""" | ||
|
||
import inspect | ||
from functools import wraps | ||
from unittest import mock | ||
|
||
|
||
def mock_envs(**envs): | ||
"""Mock environment variables for test. | ||
Can be applied to: | ||
- any function | ||
- test class - all test methods (starts with "test_*" will be decorated) | ||
""" | ||
|
||
def decorator(obj): | ||
@wraps(obj) | ||
def wrapper(*args, **kwargs): | ||
with mock.patch.dict('os.environ', envs, clear=True): | ||
obj(*args, **kwargs) | ||
|
||
if inspect.isclass(obj): | ||
for name, method in inspect.getmembers(obj, inspect.isfunction): | ||
if name.startswith('test_'): | ||
setattr(obj, name, decorator(method)) | ||
return obj | ||
else: | ||
return wrapper | ||
|
||
return decorator |