def test_run_without_context(self, testdir): source = """ import pytest def test_func(): assert True, 'some_msg' """ item: pytest.Function = testdir.getitem(source) wrapper = PytestFunctionProxy(item) transformer = AssertTransformer() transformer.context = None with pytest.raises(ContextIsRequired) as error: transformer.merge_contexts(wrapper.real_obj)
def test_ast_tree(self, testdir): source = """ def test_func(): assert True, 'some_msg' """ item: pytest.Function = testdir.getitem(source) wrapper = PytestFunctionProxy(item) code = AssertTransformer().rewrite_ast(wrapper) assert wrapper.ast_tree == code.ast_tree
def test_allow_inheritance_ctx(self, testdir): source = """ def test_func(): assert 1==2, 'some_msg' """ item: pytest.Function = testdir.getitem(source) wrapper = PytestFunctionProxy(item) AssertTransformer().rewrite_ast(wrapper) empty_transformer = EmptyTransformerWithInheritance().rewrite_ast(wrapper) assert 'my_assert' in empty_transformer.context assert 'random_object' in empty_transformer.context
def test_check_merged_contexts(self, testdir): source = """ import pytest def test_func(): assert True, 'some_msg' """ item: pytest.Function = testdir.getitem(source) wrapper = PytestFunctionProxy(item) ctx = AssertTransformer().merge_contexts(wrapper.real_obj) assert 'pytest' in ctx assert 'my_assert' in ctx
def test_class__check_ast(self, testdir): source = """ class TestX: def test_func(self): assert True, 'some_msg' """ item: pytest.Function = testdir.getitem(source) wrapper = PytestFunctionProxy(item) code = AssertTransformer().rewrite_ast(wrapper) # Module -> ClassDef -> FunctionDef -> Expr -> Call assert code.ast_tree.body[0].body[0].body[0].value.func.id == 'my_assert' assert code.ast_tree.body[0].body[0].body[0].value.args[0].value is True assert code.ast_tree.body[0].body[0].body[0].value.args[1].s == 'some_msg'
def test_class__transformed_not_found(self, testdir, mocker): mocker.patch( 'tests.transformer.AssertTransformer.exec_transformed', return_value={}, ) source = """ class TestX: def test_func(self): assert True, 'some_msg' """ item: pytest.Function = testdir.getitem(source) wrapper = PytestFunctionProxy(item) with pytest.raises(exceptions.TransformedNotFound) as error: AssertTransformer().rewrite_ast(wrapper) assert 'Class not found' in error.value.message assert 'Transformed object not found' in error.value.message
def pytest_register_ast_transformer(ast_manager: ASTManager): ast_manager.add_transformer(AssertTransformer())