def test_slash_list_with_warnings(tmpdir, no_tests): with tmpdir.join('test_file.py').open('w') as fp: _print = functools.partial(print, file=fp) _print('import warnings') _print('from slash import hooks') _print() _print('warnings.warn("Some warning")') _print('@hooks.after_session_start.register') _print('def _warn():') _print(' warnings.warn("Some warning")') if not no_tests: _print() _print('def test_me():') _print(' pass') error_stream = StringIO() args = [fp.name, '--warnings-as-errors'] if no_tests: args.append('--allow-empty') result = slash_list(args, error_stream=error_stream) errors = error_stream.getvalue() assert 'Could not load tests' not in errors assert result != 0
def test_printer_with_disalbed_colored(): report_stream = StringIO() printer = Printer(report_stream, enable_color=True) printer(_colorized('A', _style_1)) printer(_colorized('B', _style_2)) printer('C') assert report_stream.getvalue().splitlines() == ['A', 'B', 'C']
def test_slash_list_tests_without_tags(suite): suite.debug_info = False path = suite.commit() report_stream = StringIO() args = [path, '--show-tags', '--no-output'] slash_list(args, report_stream) output = report_stream.getvalue() assert not output
def setUp(self): super(ArgumentParsingTest, self).setUp() self.stderr = StringIO() self.stdout = StringIO() self.addCleanup(setattr, sys, "stderr", sys.stderr) self.forge.replace_with(sys, "stderr", self.stderr) self.addCleanup(setattr, sys, "stdout", sys.stdout) self.forge.replace_with(sys, "stdout", self.stdout)
def test_printer_with_output_disabled(force_color, enable_color): report_stream = StringIO() printer = Printer(report_stream, enable_output=False, force_color=force_color, enable_color=enable_color) printer(_colorized('A', _style_1)) printer(_colorized('B', _style_2)) printer('C') assert not report_stream.getvalue()
def test_printer_with_forced_colored(): report_stream = StringIO() printer = Printer(report_stream, force_color=True) expected_lines = [] for string, style in [('A', _style_1), ('B', _style_2), ('C', None)]: colored_string = _colorized(string, style) printer(colored_string) expected_lines.append( colored_string.colorize() if style else str(colored_string)) # pylint: disable=no-member assert report_stream.getvalue().splitlines() == expected_lines
def test_slash_list_tests(suite, should_show_tags, suite_test): suite_test.add_decorator('slash.tag("bla")') suite.debug_info = False path = suite.commit() report_stream = StringIO() args = [path, '--only-tests'] if should_show_tags: args.append('--show-tags') slash_list(args, report_stream) output = report_stream.getvalue() assert ('Tags' in output) == (should_show_tags)
def test_slash_list(suite, flag): suite.debug_info = False f = suite.slashconf.add_fixture() path = suite.commit() report_stream = StringIO() args = [path] if flag is not None: args.append(flag) slash_list(args, report_stream) assert report_stream.getvalue()
def parametrized_func(params, param_names): buff = StringIO() formatter = CodeFormatter(buff) formatter.writeln('def f({0}):'.format(', '.join(param_names))) with formatter.indented(): formatter.writeln('pass') globs = {} exec(buff.getvalue(), globs) returned = globs['f'] for param_name in param_names: returned = slash.parametrize( param_name, list(params[param_name]))(returned) return returned
def parametrized_func(params, param_names): buff = StringIO() formatter = CodeFormatter(buff) formatter.writeln('def f({}):'.format(', '.join(param_names))) with formatter.indented(): formatter.writeln('pass') globs = {} exec(buff.getvalue(), globs) # pylint: disable=exec-used returned = globs['f'] for param_name in param_names: returned = slash.parametrize(param_name, list(params[param_name]))(returned) return returned
def test_notify_if_slow_context(show_duration): output_stream = StringIO() reporter = ConsoleReporter(logbook.TRACE, output_stream) with slash.Session(console_stream=output_stream, reporter=reporter): with notify_if_slow_context('message', slow_seconds=0.1, end_message='End', show_duration=show_duration): time.sleep(1) output = output_stream.getvalue() assert 'message' in output assert 'End' in output assert ('took 0:00:01' in output) == show_duration
def test_slash_list_tests_relative_or_not(suite, relative): suite.debug_info = False path = suite.commit() report_stream = StringIO() args = [path] if relative: args.append('--relative-paths') slash_list(args, report_stream) output_lines = { _strip(line) for line in report_stream.getvalue().splitlines() } - {"Tests", "Fixtures"} assert output_lines for filename in output_lines: assert os.path.isabs(filename) == (not relative)
def test_line_truncation(long_text, use_truncation, config_override): config_override('log.truncate_console_lines', use_truncation) config_override('log.console_level', logbook.DEBUG) console = StringIO() with slash.Session(console_stream=console): logbook.debug(long_text) if use_truncation: assert long_text not in console.getvalue() assert long_text[:20] in console.getvalue() else: assert long_text in console.getvalue()
def test_slash_list_suite_file_incorrect_names(tmpdir, invalid, allow_empty): with tmpdir.join('test_file.py').open('w') as f: _print = functools.partial(print, file=f) _print('import slash') _print('class TestSomething(slash.Test):') _print(' def test_method(self):') _print(' pass') _print() _print('def test_function():') _print(' pass') function_name_remainder = method_name_remainder = '' if invalid == 'test': function_name_remainder = 'f' elif invalid == 'method': method_name_remainder = 'm' elif invalid is not None: raise NotImplementedError() # pragma: no cover error_stream = StringIO() with tmpdir.join('suitefile').open('w') as suite_file: _print = functools.partial(print, file=suite_file) _print('{}:TestSomething.test_method{}'.format(f.name, method_name_remainder)) _print('{}:test_function{}'.format(f.name, function_name_remainder)) args = ['-f', suite_file.name] if allow_empty: args.append( '--allow-empty' ) # make sure allow-empty does not affect invalid name lookup result = slash_list(args, error_stream=error_stream) if invalid is None: assert result == 0 else: assert result != 0 assert 'Could not load tests' in error_stream.getvalue() if invalid == 'test': assert 'test_functionf' in error_stream.getvalue() elif invalid == 'method': assert 'test_methodm' in error_stream.getvalue() else: raise NotImplementedError() # pragma: no cover
def _construct_fixture(self, name, scope, dependent_names): buff = StringIO() code = CodeFormatter(buff) code.writeln( 'def {0}(this, {1}):'.format(name, ', '.join(dependent_names))) with code.indented(): for dependent_name in dependent_names: code.writeln( 'tree.check_value({0!r}, {0})'.format(dependent_name)) code.writeln('@this.add_cleanup') code.writeln('def cleanup():') with code.indented(): code.writeln('tree.cleanup({0!r})'.format(name)) code.writeln('return tree.make_value({0!r})'.format(name)) globs = {'tree': self} exec(buff.getvalue(), globs) return slash.fixture(scope=scope)(globs[name])
def _construct_fixture(self, name, scope, dependent_names): buff = StringIO() code = CodeFormatter(buff) code.writeln('def {}(this, {}):'.format(name, ', '.join(dependent_names))) with code.indented(): for dependent_name in dependent_names: code.writeln( 'tree.check_value({0!r}, {0})'.format(dependent_name)) code.writeln('@this.add_cleanup') code.writeln('def cleanup():') with code.indented(): code.writeln('tree.cleanup({!r})'.format(name)) code.writeln('return tree.make_value({!r})'.format(name)) globs = {'tree': self} exec(buff.getvalue(), globs) # pylint: disable=exec-used return slash.fixture(scope=scope)(globs[name])
def test_slash_list_without_any_tests(allow_empty): empty_suite = Suite() empty_suite.debug_info = False path = empty_suite.commit() report_stream = StringIO() args = [path] if allow_empty: args.append('--allow-empty') rc = slash_list(args, report_stream) expected_rc = 0 if allow_empty else 1 assert rc == expected_rc
def run(self, verify=True, expect_interruption=False, additional_args=(), args=None, commit=True, sort=True, num_workers=1, expect_session_errors=False): if commit: self.commit() path = self._last_committed_path assert path is not None report_stream = StringIO() returned = SlashRunResult(report_stream=report_stream) captured = [] if args is None: args = [path] args.extend(additional_args) if self.is_parallel: args.extend([ '--parallel', str(num_workers), '-vvvvv', '--parallel-addr', 'localhost' ]) with self._capture_events(returned), self._custom_sorting(sort): with self._custom_slashrc(path): app = slash_run( args, report_stream=report_stream, app_callback=captured.append, ) returned.exit_code = app.exit_code if app.interrupted: assert expect_interruption, 'Unexpectedly interrupted' else: assert not expect_interruption, 'Session was not interrupted as expected' if captured: assert len(captured) == 1 returned.session = captured[0].session assert not returned.session.has_internal_errors( ), 'Session has internal errors!' if verify: validate_run(self, returned, expect_interruption=expect_interruption, expect_session_errors=expect_session_errors) return returned
def test_slash_list_with_filtering(tmpdir): with tmpdir.join('test_file.py').open('w') as fp: _print = functools.partial(print, file=fp) _print() _print('def test_a():') _print(' pass') _print('') _print('def test_b():') _print(' pass') _print('') _print('def test_c():') _print(' pass') report_stream = StringIO() args = [fp.name, '-k', 'not _b', '--only-tests'] result = slash_list(args, report_stream=report_stream) assert result == 0 listed_tests = _strip(report_stream.getvalue()).splitlines() assert listed_tests == [ '{}:{}'.format(fp.name, test_name) for test_name in ('test_a', 'test_c') ]
def run(self, verify=True, expect_interruption=False, additional_args=(), args=None, commit=True, sort=True): if commit: self.commit() path = self._last_committed_path assert path is not None report_stream = StringIO() returned = SlashRunResult(report_stream=report_stream) captured = [] with self._capture_events(returned): if args is None: args = [path] args.extend(additional_args) try: with self._custom_slashrc(path): returned.exit_code = slash_run( args, report_stream=report_stream, app_callback=captured.append, test_sort_key=self._get_test_id_from_runnable if sort else None) except (KeyboardInterrupt, SystemExit, TerminatedException) as e: if isinstance(e, KeyboardInterrupt): assert expect_interruption, 'KeyboardInterrupt unexpectedly raised' returned.exit_code = -1 returned.error_message = str(e) else: assert not expect_interruption, 'KeyboardInterrupt did not happen' if captured: assert len(captured) == 1 returned.session = captured[0].session if verify: validate_run(self, returned, expect_interruption) return returned
def test_fancy_message(long_headline, multiline): output = StringIO() reporter = ConsoleReporter(logbook.TRACE, output) headline = 'some headline here' if long_headline: headline *= 80 message = 'some message here' repetitions = 5 if multiline: message = "\n".join(message for i in range(repetitions)) message += '\n\n' reporter.report_fancy_message(headline, message) if long_headline: assert headline[:80] in output.getvalue() else: assert headline in output.getvalue() if multiline: assert output.getvalue().count(message.splitlines()[0]) == repetitions else: assert output.getvalue().count(message) == 1
def stream(): return StringIO()
def test_slash_list(): report_stream = StringIO() list_config([], report_stream) assert report_stream.getvalue()
def source(self): buff = StringIO() f = CodeFormatter(buff) self.write(f) return buff.getvalue()
def report_stream(): return StringIO()
def test_slash_list_config_with_filters(): report_stream = StringIO() list_config(['log'], report_stream) assert 'log.root' in report_stream.getvalue()
def test_slash_list_config(): report_stream = StringIO() list_config([], report_stream) assert report_stream.getvalue()
def tw(): return TerminalWriterWrapper(StringIO())