Skip to content

TDD Fundamentals

Quiz answers on this page are saved to your browser's local storage and persist between visits.

The most important aspect of TDD is.

  • Ensuring complete code coverage
  • Verifying code after it’s been written
  • Driving software development with specs

TDD was popularized under that name: “Test-Driven Development”.

Since then, many attempts have been made to refocus the attention on TDD as not being about “tests” per-se, but as a means of defining specifications for the system under test. And that’s the better perspective to take with TDD. Specifications drive the definition of the system being “right”.

Quiz Progress

0 / 0 questions answered (0%)

0 correct

Author TODO Items

At the end of this class, you should be able to…

<h1>Hello Code</h1>

The key to doing TDD is thinking about what you want to create from the perspective of an endpoint you want to achieve.

  1. Start a new project in a folder by running pnpm init followed by pnpm add -D vitest.

  2. Edit the package.json to have the "test" script run vitest.

    package.json
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
    "test": "vitest"
    }

That’s it! From here on out, it’s all about following a cycle - [Red-Green-Refactor].

Let’s model the simple idea of a fraction with a numerator and denominator: 12\frac{1}{2}. It’s going to be an object in order to preserve the numerator and denominator values. We want just a few requirements to start:

  • It must render as a string when displayed. For example: 1/2.
  • It must work with math operators when doing things like addition, multiplication, etc.
  1. Start the test runner in your VS Code terminal by typing pnpm test. At first, it will fail with a message about not having any test files.

    Run the tests
    pnpm test
    $ vitest
    DEV v5.1.0 C:/GH/dgilleland/SDEV1150/RnD/Fractions
    No test files found. You can change the file name pattern by pressing "p"
    include: **/*.{test,spec}.?(c|m)[jt]s?(x)
    exclude: **/node_modules/**, **/.git/**
  2. Let’s do a minimal setup by adding a test file: fractions.spec.js. Now the failure message will update to say that there are no tests in that file.

    Terminal window
    RERUN fractions.spec.js x1
    ❯ fractions.spec.js (0 test)
    ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
    FAIL fractions.spec.js [ fractions.spec.js ]
    Error: No test suite found in file C:/GH/dgilleland/SDEV1150/RnD/Fractions/fractions.spec.js
    ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯
    Test Files 1 failed (1)
    Tests no tests
    Start at 17:22:41
    Duration 30ms
    FAIL Tests failed. Watching for file changes...
    press h to show help, press q to quit
  3. Create two empty tests, one for each requirement we’ve identified. Note that these have a .todo in the test function as an indicator that the tests are not complete yet. On the plus side, the failure message is now replaced with a listing of the planned unit tests.

    fraction.spec.js
    import { it, expect } from 'vitest';
    it.todo('should represent the fraction 1/2 as a string', function() {
    });
    it.todo('should perform addition for two fractions', function() {
    });
    Terminal window
    ↓ fractions.spec.js (2 tests | 2 todo)
    □ should represent the fraction 1/2 as a string
    □ should perform addition for two fractions
    Test Files 1 skipped (1)
    Tests 2 todo (2)
    Start at 17:26:47
    Duration 25ms
    PASS Waiting for file changes...
    press h to show help, press q to quit
  4. Let’s work on our first requirement: Having a fraction object render as a string. Since we’re taking a TDD approach, we need to start with a failing test.

  5. Notice how our test follows an [Arrange-Act-Assert] pattern. The failure point in our test is in the “Arrange” section. We’re trying to use a function to give us a fraction object, but that function does not exist.

  6. We’ll address this first problem by creating an ultra-simple solution. First we’ll create our file that provides a fraction.

    Next, we’ll update the test file with an import statement. Now our test passes.

  7. If it seems like the prior solution is a “cheat”, you’re not wrong to think that. To move in the right direction, let’s do another version of this first test, this time with a different set of parameters for the createFraction() function.

  8. Now our test fails, and the reason should be obvious. We hard-coded a result in our solution. It’s time to fix the problem by making use of those arguments we are sending into the function.

  9. We have two passing tests - both focused on the string representation of our fraction.

  10. Now it’s time to write the test for performing math operations. We’re focusing on just addition, so we’ll make our addition intentions clear and predictable.

    14+14=0.5\frac{1}{4} + \frac{1}{4} = 0.5
  11. If we attempt a “quick” solution to just get this test to pass, we’ll discover that it breaks the other two tests.

  12. The real solution is to finally get down to the real notion of a fraction. It needs to be an object with a numerator and denominator. Let’s re-tool accordingly so that we can get the first two tests back as passing tests.

  13. Now let’s address how to do the math with fractions. The key here will be to implement a .asValue() function on the object so that the numeric conversion can happen automatically to produce a predictable result.

  14. Just to round out our ideas for supporting math operations, let’s do another one. This time, we’ll check that we can do multiplication.

    12×12=0.25\frac{1}{2} \times \frac{1}{2} = 0.25
fractions.spec.js
import { it, expect } from 'vitest';
import { createFraction } from './fraction';
it('should represent the fraction 1/2 as a string', function() {
6 collapsed lines
// Arrange
const sut = createFraction(1, 2);
// Act
const actual = sut.toString();
// Assert
expect(actual).toBe('1/2')
});
it('should represent the fraction 1/4 as a string', function() {
6 collapsed lines
// Arrange
const sut = createFraction(1, 4);
// Act
const actual = sut.toString();
// Assert
expect(actual).toBe('1/4')
});
it('should perform addition for two fractions', function() {
7 collapsed lines
// Arrange
const firstFraction = createFraction(1, 4);
const secondFraction = createFraction(1, 4);
// Act
const actual = firstFraction + secondFraction;
// Assert
expect(actual).toBe(0.5)
});
it('should perform multiplication for two fractions', function() {
7 collapsed lines
// Arrange
const firstFraction = createFraction(1, 2);
const secondFraction = createFraction(1, 2);
// Act
const actual = firstFraction * secondFraction;
// Assert
expect(actual).toBe(0.25)
});
fraction.js
export function createFraction(numerator, denominator) {
return {
toString() {
return `${numerator}/${denominator}`;
},
valueOf() {
return numerator / denominator;
}
};
}