인기있는 Unit Test 네이밍 규칙

2022. 10. 4. 17:46 Java 관련/JUnit

다음은 일반적으로 인기있는 7가지 Unit Test 명명 규칙이다.

1.  MethodName_StateUnderTest_ExpectedBehavior

예제:

  • isAdult_AgeLessThan18_False
  • withdrawMoney_InvalidAccount_ExceptionThrown
  • admitStudent_MissingMandatoryFields_FailToAdmit

2. MethodName_ExpectedBehavior_StateUndertest

예제:

  • isAdult_False_AgeLessThan18
  • withdrawMoney_ExceptionThrown_InvalidAccount
  • admitStudent_FailToAdmit_MissingMandatoryFields

3. test[Feature being tested]

예제:

  • testIsNotAnAdultIfAgeLessThan18
  • testFailToWithdrawMoneyIfAccountIsInvalid
  • testStudentIsNotAdmittedIfMandatoryFieldAreMissing

4. Feature to be tested

예제:

  • IsNotAnAdultIfAgeLessThan18
  • FailToWithdrawMoneyIfAccountIsInvalid
  • StudentIsNotAdmittedIfMandatoryFieldsAreMissing

5. Should_ExpectedBehavior_When_StateUnderTest

예제:

  • Should_ThrowException_When_AgeLessThan18
  • Should_FailToWithdrawMoney_ForInvalidAccount
  • Should_FailToAdmit_IfMandatoryFieldsAreMissing

6. When_StateUnderTest_Expect_ExpectedBehavior

예제:

  • When_AgeLessThan18_Expect_isAdultAsFalse
  • When_InvalidAccount_Expect_WithdrawMoneyToFail
  • When_MandatoryFieldsAreMissing_Expect_StudentAdmissionToFail

7. Given_Preconditions_When_SateUnderTest_Then_ExpectedBehavior

예제:

  • Given_UserIsAuthenticated_When_InvalidAccountNumberIsUsedToWithdrawMoney_Then_TransactionsWillFail

출처 : https://dzone.com/articles/7-popular-unit-test-naming