def assertScriptDoes(self, result, stdout='', stderr='', returncode=0, trim_output=True): """Fail if the result object's stdout, stderr and returncode are unexpected. *result* is usually a :class:`scripttest.ProcResult` with stdout, stderr and returncode attributes. """ if trim_output: stdout, stderr = trim(stdout), trim(stderr) self.assertEqual(returncode, result.returncode, "expected returncode %d, got %d" % (returncode, result.returncode)) self.assertEqual(result.stdout, stdout, "unexpected output on stdout") self.assertEqual(result.stderr, stderr, "unexpected output on stderr")
def assertAppDoes(self, app_cls, cmd, kwargs={}, stdout='', stderr='', status=0, raises=(), trim_output=trim): """Fail the test if the app behaves unexpectedly. *app_cls*, *cmd* and *kwargs* will be passed to :meth:`runapp`. If the application raises an :class:`Exception` instance contained in the *raises* tuple, the test will pass. Otherwise, the application's stdout, stderr and return status will be compared with *stdout*, *stderr* and *status*, respectively (using :meth:`assertEqual`). """ try: returned, app = self.runapp(app_cls, cmd, **kwargs) except raises as e: return True if trim: stdout, stderr = trim(stdout), trim(stderr) self.assertEqual(status, returned) self.assertEqual(stdout, app.stdout) self.assertEqual(stderr, app.stderr)
class AppMixin(object): """Useful methods for testing App classes. Note: This won't help for testing App _instances_. """ app_cls = None """The Application class to test.""" args = () """The arguments to pass when instantiating the test Application.""" kwargs = { "exit_after_main": False, } """The keyword arguments to pass when instantiating the test Application.""" def runapp(self, app_cls, cmd, **kwargs): """Run the application. *app_cls* is a class that inherits from :class:`cli.app.Application`. *cmd* may be a string with command line arguments. If present, *cmd* will be parsed by :func:`shlex.split` and passed to the application as its *argv* keyword argument (overriding *argv* keys in both :attr:`default_kwargs` and *kwargs*). *kwargs* will be merged with :attr:`default_kwargs` and passed to the application as well. If *stdout* or *stderr* keys are not set in either *kwargs* or :attr:`default_kwargs`, new :class:`StringIO` instances will be used as temporary buffers for application output. Returns (status, app), where *status* is the application's return code and *app* is the application instance. """ _kwargs = self.kwargs.copy() _kwargs.update(kwargs) _kwargs["stdout"] = _kwargs.get("stdout", StringIO()) _kwargs["stderr"] = _kwargs.get("stderr", StringIO()) if cmd: _kwargs["argv"] = shlex.split(cmd) app = app_cls(**_kwargs) app.setup() status = app.run() return status, app def assertAppDoes(self, app_cls, cmd, kwargs={}, stdout='', stderr='', status=0, raises=(), trim_output=trim): """Fail the test if the app behaves unexpectedly. *app_cls*, *cmd* and *kwargs* will be passed to :meth:`runapp`. If the application raises an :class:`Exception` instance contained in the *raises* tuple, the test will pass. Otherwise, the application's stdout, stderr and return status will be compared with *stdout*, *stderr* and *status*, respectively (using :meth:`assertEqual`). """ try: returned, app = self.runapp(app_cls, cmd, **kwargs) except raises, e: return True if trim: stdout, stderr = trim(stdout), trim(stderr) self.assertEqual(status, returned) self.assertEqual(stdout, app.stdout) self.assertEqual(stderr, app.stderr)