loading...
Cover image for Simple introduction to unit test

Simple introduction to unit test

bladesensei profile image coyla Updated on ใƒป2 min read

Unit test ๐Ÿงช

In computer programming, unit testing is a software testing method by which individual units of source codeโ€”sets of one or more computer program modules together with associated control data, usage procedures, and operating proceduresโ€”are tested to determine whether they are fit for use. (Wikipรฉdia)

Benefit of Unit testing (UT)

  • โœ… Verify if a change (code) has created a bug or changed the behavior somewhere in the application
  • ๐Ÿ› Find the root of the bug easily
  • ๐Ÿ“ˆ Improve software quality
  • ๐Ÿ™Œ Your teammates will thank you

A simple good test:

Has this specifications:

1. Clean code

Test code remains "code" so we should also try to respect good practices (ex: DRY, KISS, Single Responsibility... ), linters, recent langage...

๐Ÿ‘‰ In order to keep the reliability, understanding and maintainability. for your team.

2. Independence

"Unit". Should not depend on the other tests. For example: tests should be able to be run in any order.

Avoid as much as possible the dependance of other systems/code of your software/app like instantiations for example, you can use mock data or stubs to avoid this.

3. Test the necessary

I don't know if everyone will be agree with this, but ...

We don't need to test all functions/routes/libs(...) or at least not right now... ๐Ÿ˜Œ

  • Prioritize the code that you change most often.

We can be discouraged by the HUGE work to cover all our app... We can start step by step and cover just 1 small part of our app. (let me know your opinion ๐Ÿ™ )

4. Understanding ๐Ÿ“–

Naming. We should understand the test when see the test name.

This is good pattern that works for me:

[unit name] ... [should] ... [when] ... context

The name should be composed by the description, then the pattern 'should' (result we expect) and optionally 'when' that describes the scenario or context.

Tips : If the test becomes bigger, divide it into small pieces.

โž• Bonus: Phases/Structure

Must know is AAA (arrange, act, assert).

  • Arrange: prepare -> mocks, before method (), parameters(context) for our function

  • Act: basically call the function/unit work that we want to test

  • Assert: define the result that you expect

Tips : Avoid many 'expect' for only one test, to keep Single Responsibility.

Examples

I will use AVA library for tests
https://github.com/avajs/ava

A basic & Simple Test preFlight request


test('isPreFlightRequest should be true when request method is OPTIONS', (t) => {
  // ARRANGE
  const mockRequest = {
    method: 'OPTIONS',
  };

  // ACT
  const ispreFlightRequest = cors.isPreFlightRequest(mockRequest);

  //ASSERT
  t.is(ispreFlightRequest, true, 'ispreFlightRequest should return true');
});

Run

Links

You can also read this

Thank for reading ๐Ÿ™‡โ€โ™‚๏ธ

Posted on by:

Discussion

markdown guide
 

Thanks for sharing such a clean and easy to understand tutorial!