Testing Utils¶
-
class
keg.testing.
CLIBase
[source]¶ Test class base for testing Keg click commands.
Creates a CLI runner instance, and allows subclass to call
self.invoke
with command args.Class attributes: - app_cls: Optional, will default to
flask.current_app
class. - cmd_name: Optional, provides default inself.invoke
forcmd_name
kwarg.
-
class
keg.testing.
ContextManager
(appcls)[source]¶ Facilitates having a single instance of an application ready for testing.
By default, this is used in
Keg.testing_prep
.Constructor arg is the Keg app class to manage for tests.
-
classmethod
get_for
(appcls)[source]¶ Return the ContextManager instance for the given app class. Only one ContextManager instance will be created in a Python process for any given app.
-
is_ready
()[source]¶ Indicates the manager’s app instance exists.
The instance should be created with
get_for
. Only one ContextManager instance will get created in a Python process for any given app. But,get_for
may be called multiple times. The first call toensure_current
will set up the application and bring the manager to a ready state.
-
classmethod
-
keg.testing.
app_config
(**kwargs)[source]¶ Set config values on any apps instantiated while the context manager is active. This is intended to be used with cli tests where the
current_app
in the test will be different from thecurrent_app
when the CLI command is invoked, making it very difficult to dynamically set app config variables using mock.patch.dict like we normally would.Example:
class TestCLI(CLIBase): app_cls = MyApp def test_it(self): with testing.app_config(FOO_NAME='Bar'): result = self.invoke('echo-foo-name') assert 'Bar' in result.output
-
keg.testing.
inrequest
(*req_args, args_modifier=None, **req_kwargs)[source]¶ A decorator/context manager to add the flask request context to a test function.
Allows test to assume a request context without running a full view stack. Use for unit-testing a view instance without setting up a webtest instance for the app and running requests.
Flask’s
request.args
is normally immutable, but in test cases, it can be helpful to patch in args without needing to construct the URL. But, we don’t want to leave them mutable, because potential app bugs could be masked in doing so. To modify args, pass in a callable asargs_modifier
that takes the args dict to be modified in-place. Args will only be mutable for executing the modifier, then returned to immutable for the remainder of the scope.Assumes that
flask.current_app
is pointing to the desired app.Example:
@inrequest('/mypath?foo=bar&baz=boo') def test_in_request_args(self): assert flask.request.args['foo'] == 'bar' def test_request_args_mutated(self): def args_modifier(args_dict): args_dict['baz'] = 'custom-value' with inrequest('/mypath?foo=bar&baz=boo', args_modifier=args_modifier): assert flask.request.args['foo'] == 'bar' assert flask.request.args['baz'] == 'custom-value'
-
keg.testing.
invoke_command
(app_cls, *args, **kwargs)[source]¶ Invoke a command using a CLI runner and return the result.
Optional kwargs: - exit_code: Default 0. Process exit code to assert. - runner: Default
click.testing.CliRunner()
. CLI runner instance to use for invocation. - use_test_profile: Default True. Drive invoked app to use test profile instead of default.