def test_error_without_parens(): code = """ import pytest @pytest.yield_fixture() def my_fixture(): return 0 """ assert_error(FixturesVisitor, code, DeprecatedYieldFixture, config=DEFAULT_CONFIG)
def test_error_without_parens(code: str): assert_error( MarksVisitor, code, IncorrectMarkParenthesesStyle, config=DEFAULT_CONFIG, mark_name='foo', expected_parens='()', actual_parens='', )
def test_error_with_parens(): code = """ import pytest @pytest.yield_fixture def my_fixture(): return 0 """ config = DEFAULT_CONFIG._replace(fixture_parentheses=False) assert_error(FixturesVisitor, code, DeprecatedYieldFixture, config=config)
def test_error_with_parens(code: str): config = DEFAULT_CONFIG._replace(mark_parentheses=False) assert_error( MarksVisitor, code, IncorrectMarkParenthesesStyle, config=config, mark_name='foo', expected_parens='', actual_parens='()', )
def test_if_statement_same_variable_assignment_subscription(): code = dedent(""" if x == 1: y[0] = 2 else: y[0] = 4 """) assert_error(IfStatementsVisitor, code, AssignmentToSameVariable, variables='y[0]')
def test_if_statement_same_variable_assignment(): code = dedent(""" if x == 1: y = 2 else: y = 5 """) assert_error(IfStatementsVisitor, code, AssignmentToSameVariable, variables='y')
def test_error_multiple_statements(): code = """ def test_something(): with pytest.raises(AttributeError): len([]) [].size """ assert_error(RaisesVisitor, code, RaisesWithMultipleStatements, config=DEFAULT_CONFIG)
def test_if_statement_same_variable_assignment_multiple_targets(): code = dedent(""" if x == 1: y = z = 2 else: y = z = 4 """) assert_error(IfStatementsVisitor, code, AssignmentToSameVariable, variables='y, z')
def test_error_complex_statement(statement): code = f""" async def test_something(): with pytest.raises(AttributeError): {statement} True: [].size """ assert_error(RaisesVisitor, code, RaisesWithMultipleStatements, config=DEFAULT_CONFIG)
def test_if_statement_same_variable_assignment_tuple_assignment(): code = dedent(""" if x == 1: y, z = 2, 3 else: y, z = 4, 5 """) assert_error(IfStatementsVisitor, code, AssignmentToSameVariable, variables='(y, z)')
def test_if_statement_same_variable_assignment_attribute(): code = dedent(""" if x == 1: y.a.b = 2 else: y.a.b = 4 """) assert_error(IfStatementsVisitor, code, AssignmentToSameVariable, variables='y.a.b')
def test_error(): code = """ import pytest def test_xxx(): self.assertEqual(1, 1) """ assert_error(UnittestAssertionVisitor, code, UnittestAssertion, assertion='assertEqual')
def test_error(): code = """ import pytest @pytest.fixture(scope='function') def my_fixture(): return 0 """ assert_error(FixturesVisitor, code, ExtraneousScopeFunction, config=DEFAULT_CONFIG)
def test_error_with_parens(): code = """ import pytest @pytest.mark.usefixtures() def test_something(): pass """ assert_error(MarksVisitor, code, UseFixturesWithoutParameters, config=DEFAULT_CONFIG)
def test_error(): code = """ import pytest def test_something(): with pytest.raises(): pass """ assert_error(RaisesVisitor, code, RaisesWithoutException, config=DEFAULT_CONFIG)
def test_error_no_parens(): code = """ import pytest @pytest.mark.usefixtures def test_something(): pass """ config = DEFAULT_CONFIG._replace(mark_parentheses=False) assert_error(MarksVisitor, code, UseFixturesWithoutParameters, config=config)
def test_if_statement_same_variable_assignment_tuple_within_tuple_assignment(): code = dedent(""" if x == 1: a = y, (z, w) = 2, (3, 9) else: a = y, (z, w) = 4, (5, 11) """) assert_error( IfStatementsVisitor, code, AssignmentToSameVariable, variables='a, (y, (z, w))', )
def test_error_try(): code = """ def test_something(): with pytest.raises(AttributeError): try: [].size except: raise """ assert_error(RaisesVisitor, code, RaisesWithMultipleStatements, config=DEFAULT_CONFIG)
def test_if_statement_same_variable_assignment_slice(): code = dedent(""" if x == 1: y[1:5:2] = 2 else: y[1:5:2] = 4 """) assert_error( IfStatementsVisitor, code, AssignmentToSameVariable, variables='y[1:5:2]', )
def test_if_statement_same_variable_assignment_list_assignment(): code = dedent(""" if x == 1: [y, [z, w]] = 2, (3, 9) else: [y, [z, w]] = 4, (5, 11) """) assert_error( IfStatementsVisitor, code, AssignmentToSameVariable, variables='[y, [z, w]]', )
def test_error_after_no_parens(): code = """ import pytest @pytest.fixture() @pytest.mark.asyncio async def my_fixture(): return 0 """ assert_error(FixturesVisitor, code, UnnecessaryAsyncioMarkOnFixture, config=DEFAULT_CONFIG)
def test_error_return(): code = """ import pytest @pytest.fixture() def my_fixture(request): resource = acquire_resource() request.addfinalizer(resource.release) return resource """ assert_error(FixturesVisitor, code, FixtureFinalizerCallback, config=DEFAULT_CONFIG)
def test_error_simple(): code = """ import pytest @pytest.fixture() def patch_something(mocker): mocker.patch('some.thing') """ assert_error( FixturesVisitor, code, MissingFixtureNameUnderscore, name='patch_something', config=DEFAULT_CONFIG, )
def test_error_mixed(): code = """ import pytest @pytest.fixture('module', autouse=True) def my_fixture(): return 0 """ assert_error( FixturesVisitor, code, FixturePositionalArgs, name='my_fixture', config=DEFAULT_CONFIG, )
def test_error_after(): code = """ import pytest @pytest.fixture() @pytest.mark.usefixtures('a') def my_fixture(): return 0 """ assert_error( FixturesVisitor, code, ErroneousUseFixturesOnFixture, config=DEFAULT_CONFIG, )
def test_error_with_return(): code = """ import pytest @pytest.fixture() def _my_fixture(mocker): return 0 """ assert_error( FixturesVisitor, code, IncorrectFixtureNameUnderscore, name='_my_fixture', config=DEFAULT_CONFIG, )
def test_error_match_is_empty(match): code = f""" import pytest def test_something(): with pytest.raises(ValueError, match={match}): raise ValueError("Can't divide 1 by 0") """ assert_error( RaisesVisitor, code, RaisesWithoutMatch, config=DEFAULT_CONFIG, exception='ValueError', )
def test_error_no_argument_given(exception): code = f""" import pytest def test_something(): with pytest.raises({exception}): raise ValueError("Can't divide 1 by 0") """ assert_error( RaisesVisitor, code, RaisesWithoutMatch, config=DEFAULT_CONFIG, exception=exception, )
def test_error_without_parens(): code = """ import pytest @pytest.fixture def my_fixture(): return 0 """ assert_error( FixturesVisitor, code, IncorrectFixtureParenthesesStyle, config=DEFAULT_CONFIG, expected_parens='()', actual_parens='', )
def test_error(): code = """ import pytest @pytest.fixture() def my_fixture(): resource = acquire_resource() yield resource """ assert_error( FixturesVisitor, code, UselessYieldFixture, name='my_fixture', config=DEFAULT_CONFIG, )