DEV Community

Francois MUGOROZI
Francois MUGOROZI

Posted on

Test-Driven Development (TDDF) For API

Alt Text

TDD

Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: requirements are turned into very specific test cases, then the code is improved so that the tests pass. This is opposed to software development that allows code to be added that is not proven to meet requirements.Wkipedia

Why TDD

You can't imagine how TDD is very useful in software development industries. Most individuals programmers usually do not use this approach. However, the trusted and scalable product has to be tested for any expected cases. Hare are some of the benefits of TDD:

  • Test-driven development offers more than just simple validation of correctness, but can also drive the design of a program
  • It allows a programmer to focus on the task at hand as the first goal is to make the test pass
  • TDD can lead to more modularized, flexible, and extensible code.

You can not talk about TDD without talking about CI/CD

Alt Text
CI is a coding philosophy and set of practices that drive development teams to implement small changes and check-in code to version control repositories frequently. Because most modern applications require developing code in different platforms and tools, the team needs a mechanism to integrate and validate its changes.Read more

Without talking to much, let see this in practice. For this demo, I am going to create a simple contacts book API.

  • Prerequisites:
    • Node js and Express
    • Creating API.
    • Git and Github

Now I will start

  • Step 1:

    • Create Repository on Github mine is "tdd-demo-ass" and clone it to your computer.
    • Create an account on Travis and connect your repository with Travis, after connecting it you have to copy the markdown and add it to your README file.Read more here
    • Create account on coveralls.io and connect your repo.Coveralls
  • Step 2:

    Now that we have Travis, it will help us for CI and then we need where to deploy our app after a successful build, here we can use Heroku.

    To be able to perform in continuous delivery, we create pipelines which help to deploy our codes to the codebase automatically when all test pass.

Go to Heroku, create an account, and then create a new pipeline and connect it with your GitHub repo. From there, you can have a review app (where you can test every pull request before merging into the base branch), staging app which you can use whole testing and production app where the final app gets deployed when everything is ready.

  • Step 3:

Now we have everything set up, we will start by writing our test cases, which by now will fail.
This is the structure of mocha and chai testing



```
const chai = require('chai');
const { expect } = require('chai');
const { describe, it } = require('mocha');
const chaiHttp = require('chai-http');
const { app } = require('../app');

chai.should();
chai.use(chaiHttp);

describe('Route test', () => {
  it('Should return 404 not found route', (done) => {
    chai
      .request(app)
      .get('/api/notfound')
      .end((err, res) => {
        expect(res).to.have.status(404);
        done();
      });
  });

  it('Should return 200 on / request route', (done) => {
    chai
      .request(app)
      .get('/')
      .end((err, res) => {
        expect(res).to.have.status(200);
        done();
      });
  });
});
```


Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cuyh9bbwpbcemekfxcs4.JPG)
Alt Text
From here when we create a pull request of our first branch to merge it into our base branch, the Travis tests fail and the code can not be deployed
Alt Text

It may sound reckless to start with writing tests, but believe me this will save us from a lot of troubles and also keep our codes maintainable so that none can mess with it in the future.

  • Step 4:

No, it's time to write codes that pass the test. I will create contacts book API with basic CRUD operations and authentication. when I am done, this is what I get when I run the test again!Alt Text
And now when the PR is merged, my app automatically get deployed through the pipeline we created on Heroku. Alt Text and Travis and Coverage get 100% Alt Text

Conclusion

Now that we have an idea of what happens in CI/CD and TDD development, I believe that this is a required skill for every developer for creating effective and maintainable codes.

Remember, every API needs documentation, you can get this demo API doc Here.

You can also get the codes for this demo on GitHub.

Top comments (0)