Wednesday, May 08, 2024

TDD @ IAG

  • Java-based test automation framework.
  • For each new feature expected to write an automation piece of code to validate the functionality. 
  • Expected to carry out TDD. Write a failed test case, and then write the TC to check functionality.
  • Not the same example as below, but this sort of gives an idea of what I did there.
  1. Add test [You don't write test for the code you are writing, rather you write the test code for the functionatliy you want to achieve] for the new functionality to be implemented. The tests serves as an [executable] specification. 
  2. That is you write one test, then implement enough code to pass that test, then refactor the existing code and test(s) if necessary, and then maybe commit or integrate before you get too much divergence from the main branch.
  3. Run the tests and confirm that the newly added tests fail.
  4. Implement the new functionality.
  5. Run all tests to confirm that they all pass.
  6. Optional: refactor the code to tidy things up now you know more.

// FactorialTest.java

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class FactorialTest {

    @Test
    public void testFactorial() {
        // Test case for factorial of 5
        assertEquals(120, Factorial.factorial(5));
    }
}

// Factorial.java

public class Factorial {

    public static int factorial(int n) {
        if (n < 0)
            throw new IllegalArgumentException("Factorial is not defined for negative numbers");
        else if (n == 0)
            return 1; // Base case: 0! = 1
        else
            return n * factorial(n - 1); // Recursive call for n!
    }
}

> assertEquals tests that factorial of 5 is equal to 120. It will fail when Factorial.java is not written. 

No comments:

Post a Comment

DSPM, Data Security Posture Management, Data Observability

DATA SECURITY POSTURE MANAGEMENT DSPM, or Data Security Posture Management, is a practice that involves assessing and managing the security ...