Git Product home page Git Product logo

tdd_dojo's Introduction

Testing

Welcome to tdd_dojo, this repository shows concepts and exercises related to TDD, clean code and automated testing.

Project Setup

Install Python

Before starting, make sure you have installed Python. In this tutorial, Python 3.8 is used.

Setting up virtual environment

It is a good practice to isolate the project that we are working on from the rest of the system. You can use venv or pyenv

Install Pytest

For testing we're going to use pytest as a testing framework. so you can install this tool with:

pip install -r requirements.txt

Testing

Naming the test

There is no rule when naming test but your test will be more Self-documenting if you have this parts:

  • Name of the method or function you are testing
  • What the method or function is supposed to do
  • Under what circumstances

Example:

def test__sum_two_numbers_function__returns_twelve__when_inputs_are_five_and_seven():
          ------------------------  --------------  ------------------------------
                    |                     |                       |
                    |                     |                       |
                    |                     |                       |
           Name of the function.     What it does.    Conditions of the arguments.

Note: Double underscore is used between each part.

Parts of a test:

To make a test more readable, you can divide your test in:

  • Setup (creating mocks, stubs or the values of the arguments)
  • Execution (calling the method or function)
  • Assertion (assert that mocks were called and right values are returned)

Example:

def test__sum_two_numbers_function__returns_twelve__when_inputs_are_five_and_seven():
    #Setup
    number_1 = 5
    number_2 = 7

    #Execution
    result = sum_two_numbers(number_1, number_2)

    #Assertions
    assert result == 12

Note: There is one space between the parts of the test.

Happy Path and Sad Path

Is recommended to consider first what users are supposed to do when using our application and write test for this scenarios, this is called "happy path". After that, testing the "sad paths", or the many ways that users can break our app is important, since it will help handling errors.

Test Driven Development

Test Driven Development, shortened as TDD, is a set of techniques that “encourages simple designs and test suites that inspire confidence”. To use TDD, your work should be divided in the simplest steps possible, each step is focused on only one task. Every step should follow this algorithm:

  1. Write a test for the new function or piece of code that you want to implement. The test should fail.
  2. Write only enough code to pass this test.
  3. Refactor the code.
  4. Goto 1

drawing

Workshops

Workshop 1

In the first example we are going to create a calculator using TDD. To do the example by yourself, goto Example.

If you want to check the answer for this dojo, do:

git checkout Exercise_1_Solution

Workshop 2

In the second example we are going to build the Game of life using TDD. To do the example by yourself, goto Example.

If you want to check the answer for this dojo, do:

git checkout Exercise_2_Solution

Workshop 3

In the workshop 3, we are going to create a calculator similar to what we did on Example 1, but with one assumption: There is another team working in a dependency called "calculator" that we need to use. To follow this example, goto Example.

If you want to check the answer for this dojo, do:

git checkout Exercise_3_Solution

tdd_dojo's People

Contributors

lenshinoda avatar eguezgustavo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.