ServiceNow-ATF

What is Test-Driven Development (TDD)?

Programmers shouldn’t have to guess whether software is working correctly.

They should be able to prove it, every step of the way.

~ Neelam Dwivedi (Associate Professor at University of Oklahoma)

TDD Approach

Test-Driven Development (TDD) is a software development approach where tests are written before the actual code.

Test-Code-Refactor Cycle

Test-driven development (TDD) is a software development process that relies on the repetitive cycle of writing

Unit Testing Focus

Regression testing

Benefits of TDD

TDD Best Practices

Test-Driven Development is a powerful methodology that can lead to higher-quality software and a more efficient development process.

By following the Red-Green-Refactor cycle and adhering to best practices, developers can create robust, maintainable code that meets user requirements.

Test structure

Test Class and Code Class Relationship

Arrange, Act, Assert Pattern in Test Cases

Each test case typically follows this pattern:

  1. Arrange: Set up the elements required for the test.
  2. Act: Perform the action you want to test.
  3. Assert: Verify that the result is as expected.
    • Assertions are used in programming to ensure that a certain condition is true at a specific point in the program.
    • If the condition is false, an assertion error is thrown.

Avoiding Code Repetition

Test Fixtures

To avoid repeating setup code across multiple test cases, you can use test fixtures.
These are methods that set up a fixed state for your tests:

Shared Fixtures

Test doubles

Test doubles are used to simulate the behavior of external dependencies (like databases or web services) that may not be available or ready during testing.

Mock Objects

Spy

Stub

A test double method that returns a value, feeding desired inputs into the tests rather than reflecting real behavior

Dependency Injection

This is a design pattern that allows you to pass dependencies to your code instead of hardcoding them

Further Reading

  1. Programming Foundations: Test-Driven Development
  2. Refactoring
  3. Continuous Integration and Continuous Delivery (CI/CD) Fundamentals - v
  4. Source Control Fundamentals
  5. Setting up a pipeline using CI/CD APIs (GitHub example)
  6. Automate your CI/CD Pipeline using Github Actions