Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deciding whether a test is a Unit or Integration test

So I'm trying to decide the way to plan and organize a testing suite for my python project but I have a doubt of when a unit test is no longer a unit test and I would love to have some feedback from the comunity.

If I understand correctly:

  • A Unit test test a minimal part of your code, be if a function/method that does one and only one simple thing, even if it has several use cases.
  • An Integration test tests that two or more units of you code that are executed under the same context, environment, etc (but trying to keep it to a minimmum of units per integration test) work well together and not only by themselves.

My doubt is: say I have a simple function that performs a HTTP request and returns the content of such request, be it HTML, JSON, etc, it doesn't matter, the fact is that the function is very, very simple but requests information from an external source, like:

import requests

def my_function(arg):
    # do something very simple with `arg`, like removing spaces or the simplest thing you can imagine
    return requests.get('http://www.google.com/' + arg).content

Now this is a very stupid example, but my doubt is:

Given that this function is requesting information from an external source, when you write a test for it, can you still consider such test a Unit test?

UPDATE: The test for my_function() would stub out calls to the external source so that it doesn't depend on network/db/filesystem/etc so that it's isolated. But the fact that the function that is being tested depends on external sources when running, for example, in production.

Thanks in advance!! :)

P.S.: Of course Maybe I'm not understading 100% de purposes of Unit and Integration testing, so, if I'm mistaken, please point me out where, I'll appreciate it.

like image 625
Gerard Avatar asked Nov 10 '22 07:11

Gerard


1 Answers

Based on your update:

The test for my_function() would stub out calls to the external source so that it doesn't depend on network/db/filesystem/etc so that it's isolated. But the fact that the function that is being tested depends on external sources when running, for example, in production.

As long as the external dependencies are stubbed out during your test then yes you can call it a Unit Test. It's a Unit Test based on how the unit under test behaves in your test suite rather than how the unit behaves in production.

Based on your original question:

Given that this function is requesting information from an external source, when you write a test for it, can you still consider such test a Unit test?

No, any test for code that touches or depends on things external to the unit under test is an integration test. This includes the existence of any web, file system, and database requests.

If your code is not 100% isolated from it's dependencies and not 100% reproducible without other components then it is an integration test.

For your example code to be properly unit tested you would need to mock out the call to google.com.

With the code calling google.com, your test would fail if google went down or you lost connection to the internet (ie the test is not 100% isolated). Your test would also fail if the behavior of google changed (ie the test is not 100% reproducable).

like image 180
mezoid Avatar answered Nov 15 '22 07:11

mezoid