Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-5961

Test methods should not contain too many assertions

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Refactor this method to reduce the number of assertions from {number of assertions} to less than {default value}.
    • Highlighting:
      Hide

      Primary locations:

      • Method name

      Secondary locations:

      • Assertions
      Show
      Primary locations: Method name Secondary locations: Assertions
    • List of parameters:
      Hide

      Key: maximumAssertionNumber
      Description: The maximum authorized number of assertions in a test method.
      Default Value: 25

      Show
      Key: maximumAssertionNumber Description: The maximum authorized number of assertions in a test method. Default Value: 25
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      20min
    • Analysis Scope:
      Test Sources

      Description

      A common good practice is to write test methods targeting only one logical concept, that can only fail for one reason.
      While it might make sense to have more than one assertion to test one concept, having too many is a sign that a test became too complex and should be refactored to multiples ones.

      This rule will report any test method containing more than a given number of assertion.

      Noncompliant Code Example

      With a parameter of 2.

      @Test
      void test() { // Refactor this method.
        assertEquals(1, f(1));
        assertEquals(2, f(2));
        assertEquals(3, g(1));
      }
      

      Compliant Solution

      void test_f() {
        assertEquals(1, f(1));
        assertEquals(2, f(2));
      }
      void test_g() {
        assertEquals(3, g(1));
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              quentin.jaquier Quentin Jaquier
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: