Click here to Skip to main content
15,884,472 members
Articles / DevOps
Tip/Trick

Software Testing!! Naming Convention of Test Method for Unit Testing

Rate me:
Please Sign up or sign in to vote.
4.56/5 (6 votes)
13 Apr 2017CPOL5 min read 11.4K   9  
Naming Convention of Test Method for Unit Testing using Behavior Driven Development (BDD)

Before reading this topic, it is recommended to read --"Software Testing!! Best Practice and Principles to Write Unit Testing"

3.3 Writing Test Scenario, Test Case and Test Data

3.3.1 Only Three Test Cases for Unit Testing, Nothing Else

  1. Positive Test Cases: Correct data to check for correct output.
  2. Negative Test Cases : Broken or missing data to check for proper handling
  3. Exception Test Cases: Giving unexpected data or behavior and check for the exception caught properly or not.

3.3.2 Test Data According to The Test Cases

Let’s set some test data and consider “GetSum()” as an example. Here my main goal to show you how we can set some Test Data for the test cases.

3.3.2.1Type of Test Data

  1. Positive Data
  2. Negative Data
  3. Exceptional Data
3.3.2.1.1 Positive Data for positive Test Cases

The main goal of the positive Test cases, to verify the functionality of the logics.

TestCase-1: Given positive values, should return expected result

  • TestData-1: Set Input Parameters as firstNumber =1, secondNumber=1
3.3.2.1.2 Negative Data for Negative Test Cases:

The main goal of the negative Test cases, to get proper messages for the bad input according to your business requirements. 

TestCase-2: Given invalid values, should produce invalid argument message

  • TestData-2: Set Input Parameters as firstNumber =-1, secondNumber =-1
  • TestData-3: Set Input Parameters as firstNumber =-1, secondNumber = 1
  • TestData-4: Set Input Parameters as firstNumber = 0, secondNumber = 1
  • TestData-5: Set Input Parameters as firstNumber = 0, secondNumber =-1
  • TestData-6: Set Input Parameters as firstNumber = 0, secondNumber = 0 etc.….
3.3.2.1.3 Exceptional Data for Exception Test Cases

The main goal of the Exceptional Test cases, to find out the proper exception handling with proper messages. So that your code can’t break for the threshold limits.

Here you can set the threshold limits of your test data. In DOT NET, the minimum value for a variable of type int is -2147483648 and the maximum value for a variable of type int is 2147483647.

TestCase3: Given threshold limit values, should throw exception message

  • TestData-7: Set Input Parameters as firstNumber =1, secondNumber =2147483649
  • TestData-8: Set Input Parameters as firstNumber =1, secondNumber = -2147483649
  • TestData-9: Set Input Parameters as firstNumber =2147483649 secondNumber = -2147483649
  • TestData-10: Set Input Parameters as firstNumber =2147483647, secondNumber=2147483647 etc.….

3.4 Naming Convention of Test Method for Unit Testing

3.4.1 Traditional Principle of Unit Test

One Test Method is written to test one and only one method and one assert method should test only one expectation at a time.

In a short the principle says – “one function/method and one assert per test method”

So, let’s consider the bellow example

3.4.1.1 Comparing the Traditional Principle to the Real World

3.4.1.1.1 Test Scenario

Verify the “GetSum” Method

3.4.1.1.2 Test Cases
3.4.1.1.2.1 Positive Test Cases

TC1: Given positive values, should return expected result

Test Data-1: firstValue =5, secondValue =6

3.4.1.1.2.2Negative Test Cases

TC2: Given zero values, should produce invalid argument message

Test Data-2: firstValue =0, secondValue =0

TC3: Given negative values, should produce invalid argument message

Test Data-3: firstValue =-5, secondValue =-6

3.4.1.1.2.3Exceptional Test Cases

TC4: Given threshold limit values, should throw exception message

Test Data-4: firstValue =2147483647, secondValue =2147483647

3.4.1.1.3Test Method Example

Now according to the traditional principle, let’s write the test method for the “GetSum”

Now according to the traditional principle we have covered the Positive Test Case with “Test Data-1”. But what about negative and exceptional test cases??

How do we cover the negative and exceptional test cases with traditional principle??

3.4.2Behavior Driven Development (BDD)

3.4.2.1 Why Need BDD

If we want to cover all of the behaviors of our test cases according to our previous example then we need to follow some technique so that we can write down all of the behaviors of the method. So, The BDD is the technique which gives us the opportunity to fulfillment all of the test cases with standard and readable naming convention. Many peoples, many minds. There are many techniques to write the naming convention of the test method. But if really depends on you and your preference. There is nothing right or wrong if you follow some other technique. Anyway, in a short we can say that In BDD, components test their expected behavior.

3.4.2.2 Concept of BDD

  1. Given I am a beginner to the BDD technique
    And I never use this technique before
  2. When I read this tutorial for BDD
  3. Then I have started to like it and I have started to use this
    And finally I learn it.

3.4.2.3 BDD Naming convention

3.4.2.3.1Test Scenario

Verify the “GetSum” Method

3.4.2.3.2 Test Cases
3.4.2.3.2.1 Positive Test Cases

TC1: Given positive values, should return expected result

Test Data-1: firstValue =5, secondValue =6

Test Method - Naming Convection:

  • GivenPositiveVaidValuesAsParams_WhenGetSumIsCalled_ThenItShouldReturnSumValue

More Readable -

  • Given_Positive_Vaid_Values_As_Params_When_GetSum_Is_Called_Then_It_Should_Return_Sum_Value

3.4.2.3.2.2 Negative Test Cases

TC2: Given zero values, should produce invalid argument message

Test Data-2: firstValue =0, secondValue =0

Test Method - Naming Convection:

GivenZeroValuesAsParams_WhenGetSumIsCalled_ThenItShouldThrowInvalidArgumentException

More Readable -

Given_Zero_Values_As_Params_When_GetSum_IsCalled_Then_It_Should_Throow_Invalid_Argument_Exception

TC3: Given negative values, should produce invalid argument message

Test Data-3: firstValue =-5, secondValue =-6

Test Method - Naming Convection:

GivenNegativeValues_WhenGetSumIsCalled_ThenItShouldThrowInvalidArgumentException

More Readable -

Given_Negative_Values_When_GetSum_Is_Called_Then_It_Should_Throw_Invalid_Argument_Exception

3.4.2.3.2.3Exceptional Test Cases

TC4: Given threshold limit values, should throw exception message

Test Data-4: firstValue =2147483647, secondValue =2147483647

GivenMaxLimitValuesOfIntAsParams_WhenGetSumIsCalled_ThenItShouldThrowSumException

More Readable -

Given_Max_Limit_Values_Of_Int_As_Params_When_GetSum_IsCalled_Then_It_Should_Throw_Sum_Exception

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
United States United States
Lazy software engineer, don’t believe in hard work.

Comments and Discussions

 
-- There are no messages in this forum --