def filename(is_class, a_values, fixture_values, tmpdir): returned = str(tmpdir.join('testfile.py')) with open(returned, 'w') as f: with ExitStack() as stack: code = CodeFormatter(f) code.writeln('import slash') code.writeln('@slash.fixture') code.writeln( '@slash.parametrize("value", {})'.format(fixture_values)) code.writeln('def fixture(value):') with code.indented(): code.writeln('return value') if is_class: code.writeln('class Test(slash.Test):') stack.enter_context(code.indented()) code.writeln('@slash.parametrize("a", {})'.format(a_values)) code.write('def test_1(') if is_class: code.write('self, ') code.writeln('a, fixture):') with code.indented(): code.writeln('pass') return returned
def filename_test_fixture(tmpdir): returned = str(tmpdir.join('testfile.py')) with open(returned, 'w') as f: with ExitStack() as stack: code = CodeFormatter(f) code.writeln('import slash') code.writeln('@slash.fixture') code.writeln('@slash.requires({}, {})'.format( _UNMET_REQ_DECORATOR, '"msg1"')) code.writeln('def fixture():') with code.indented(): code.writeln('return 1') code.writeln('@slash.fixture(autouse=True)') code.writeln('@slash.requires({}, {})'.format( _MET_REQ_DECORATOR, '"msg2"')) code.writeln('def fixture1():') with code.indented(): code.writeln('return 1') code.writeln('class Test(slash.Test):') stack.enter_context(code.indented()) code.write('def test_1(') code.write('self, ') code.writeln('fixture):') with code.indented(): code.writeln('pass') return returned
def test_debugger_called_on_hooks(hook_exception, request, forge, config_override, checkpoint, debug_enabled): hook_name, exception_type, should_raise = hook_exception @gossip.register(hook_name) def raise_exc(): raise exception_type() config_override("debug.enabled", debug_enabled) def test_something(): pass forge.replace_with(slash.utils.debug, 'launch_debugger', checkpoint) with ExitStack() as exception_stack: if should_raise: exception_stack.enter_context(pytest.raises(exception_type)) with slash.Session() as s: with s.get_started_context(): slash.runner.run_tests(make_runnable_tests(test_something)) assert checkpoint.called == debug_enabled if debug_enabled: assert checkpoint.args[0][0] is exception_type assert type(checkpoint.args[0][1]) is exception_type # pylint: disable=unidiomatic-typecheck
def run_tests_in_session(test_class_path_or_iterator, session=None): with ExitStack() as stack: if session is None: session = slash.Session() stack.enter_context(session) test_class_path_or_iterator = make_runnable_tests( test_class_path_or_iterator) with session.get_started_context(): slash.runner.run_tests(test_class_path_or_iterator) for result in session.results.iter_test_results(): for err in itertools.chain(result.get_errors(), result.get_failures(), result.get_skips()): _logger.debug("Unsuccessful result: {0}", err) return session
def test_handling_exceptions_inside_assert_raises_with_session(with_session): value = CustomException() with ExitStack() as ctx: if with_session: session = ctx.enter_context(slash.Session()) ctx.enter_context(session.get_started_context()) else: session = None with slash.assert_raises(CustomException): with exception_handling.handling_exceptions(): raise value assert not exception_handling.is_exception_handled(value) if with_session: assert session.results.get_num_errors() == 0
def test_handling_exceptions_inside_allowing_exceptions_with_session( with_session): value = CustomException() with ExitStack() as ctx: if with_session: session = ctx.enter_context(slash.Session()) ctx.enter_context(session.get_started_context()) else: session = None with logbook.TestHandler() as handler: with slash.allowing_exceptions(CustomException): with exception_handling.handling_exceptions(): raise value assert not exception_handling.is_exception_handled(value) if with_session: assert session.results.get_num_errors() == 0 assert len(handler.records) == 0 # pylint: disable=len-as-condition