/ dev / clint / tests / rules / test_no_class_based_tests.py
test_no_class_based_tests.py
 1  from pathlib import Path
 2  
 3  from clint.config import Config
 4  from clint.index import SymbolIndex
 5  from clint.linter import Position, Range, lint_file
 6  from clint.rules.no_class_based_tests import NoClassBasedTests
 7  
 8  
 9  def test_no_class_based_tests(index: SymbolIndex) -> None:
10      code = """import pytest
11  
12  # Bad - class-based test with test methods
13  class TestSomething:
14      def test_feature_a(self):
15          assert True
16  
17      def test_feature_b(self):
18          assert True
19  
20      def helper_method(self):
21          return 42
22  
23  # Bad - another class-based test
24  class TestAnotherThing:
25      def test_something(self):
26          pass
27  
28  # Good - class without test methods (utility class)
29  class HelperClass:
30      def helper_function(self):
31          return 42
32  
33      def setup_something(self):
34          pass
35  
36      def test_something(self):
37          pass
38  
39  # Good - function-based test
40  def test_valid_function():
41      assert True
42  
43  # Good - regular function
44  def helper_function():
45      return 42
46  """
47      config = Config(select={NoClassBasedTests.name})
48      violations = lint_file(Path("test_something.py"), code, config, index)
49      assert len(violations) == 2
50      assert all(isinstance(v.rule, NoClassBasedTests) for v in violations)
51      assert violations[0].range == Range(Position(3, 0))  # TestSomething class
52      assert violations[1].range == Range(Position(14, 0))  # TestAnotherThing class
53  
54  
55  def test_no_class_based_tests_non_test_file(index: SymbolIndex) -> None:
56      code = """import pytest
57  
58  # This should not be flagged because it's not in a test file
59  class TestSomething:
60      def test_feature_a(self):
61          assert True
62  """
63      config = Config(select={NoClassBasedTests.name})
64      violations = lint_file(Path("regular_file.py"), code, config, index)
65      assert len(violations) == 0