简体   繁体   中英

Integration vs Unit tests for APIs

Can we consider sending a request to an endpoint a unit or an integration test?

import lib from 'testing-lib';
// ...
const { testClient, expect } = lib;
const response = testClient
    .request(app)
    .get('/test/endpoint/');
// ...

expect(response).fulfills.some.condition.ok

I have a feeling that this is an integration test because it will ascertain that every piece, that is in between the request going out and the response coming back, is working as expected. I need to know if my vague understanding is correct or if I am missing some details.

Yeap this is more an integration test.

Unit tests are more like the functional business logic test. For an example after your route handler (controller) received the request and calls Service to handle the logic. Test of that logic is a unit test.

Integration test is checking if data-flow works fine.

I'm gonna go with neither. It's a functional test.

Unit tests test units of code. Hence the name. A unit of code is typically a function, class or module of some sort.

Integration tests validate that our units of code work together as expected. But it's still just testing the code.

Functional tests test the actual software in a deployed state through exposed interfaces.

So, in Node, a unit test might be testing one of your JavaScript modules by itself and mock out the dependencies. An integration test would be testing that your modules work together and would mock out only the extreme edges of the system. And a functional test would test that a particular endpoint works over HTTP and wouldn't mock anything.

I'll add that I encourage sticking to DRY principles while writing your tests. If you have a unit test that validates a thing, you don't need to validate that in an integration test. Just validate that the units work together as expected. And the same with functional tests. Don't validate the integration, that the units work together. Validate that the endpoints map to the expected behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM