Difference between functional test and integration test

functional-testingintegration-teststesting

I am deeply confused the difference. I've read so many definitions and they always explain functional test as testing the requirement is satisfied. Well, that's just rephrasing the name functional test. That doesn't clarify the difference.

I am interested in some real code to demonstrate the difference.

Suppose we have a function in a library performs hashing:

def custom_hasher(scheme, val):
    # use many hashing libraries...


def hasher(inlist, scheme):
    """ Take a list and outputs the hashed values of a list. """

    output = list()
    for val in inlist:
        output.append(custom_hasher(scheme, val))
    return output

Now for a functional test, I am guessing we want to test ['a', 'b'] is returned as something like ['jask34sdasdas', 'asasjdk234sjdk'] given some scheme.

But that's just about what an integration test can do! I know exactly what type input I want (I want good execution so I pass in a list), or I want it to raise Object has no append method exception if I pass in a dictionary.

I can do that in both. Where's the distinction?

Another example is some web app:

@logged_in   # only logged in user can do this
@route("/invite", method=['POST'])
def send_invite(request):
   recp_email = request.data['recp_email']

   # now do a bunch of logics before and after sending an email

So in my integration test, I will definitely do this over a network (have the server running). Send some request to this url. Same for functional test.

How to draw a line? For this case, I can write a functional test that tests to find an email is sent by looking at the send log in some table. But that's a different function than what I am testing (the view send_invite).

So I don't see how to differentiate the two. They both assert something.

Please help.

Best Answer

Functional tests are generally used as a synonym for integration tests. You may be confusing the unit tests with functional tests if I understood your question correctly.

In unit tests, aim to test the function itself in isolation, by mocking/stubbing other components.

In integration tests, test different systems working together, to see if they work together correctly to produce the correct output.

For your example send_invite, in unit tests, you can mock the email and other framework components, and just test if the send invite function correctly calls the email component with correct arguments. For the integration tests, you can set up a testing server and see if the function correctly sends emails and does everything it supposed to do, including the other components.

Unit tests are the smallest testing block, followed by the integration tests.


EDIT:

Let's leave the words aside and approach the problem as why we need tests. We need tests to ensure our code is working correctly.

In the send_email example, first we need to test the function in isolation to see it does what it supposed to do. We do this by isolating the function from other components by mocking/stubbing to test the function itself. Assuming we have a different component that is responsible for sending emails, and our function is calling this component to send emails, if our email component is broken, we won't want this to affect the test of send_email, it's not directly related to this function. We just want to make sure the code in the send_email function is working correctly. That is generally called unit testing.

After we are sure the function is working correctly in isolation, we need to test if the invitation system is working correctly, with all of it's components. This time we do it by testing the function without mocks/stubs. This is generally called integration tests.

In some places, there are functional tests with side effects and functional tests without side effects, which means calling the function with predefined arguments in tests without mocking/stubbing to see it produces the correct output, and calling the function with predefined arguments in tests with mocking/stubbing other components, respectively. This can translates into unit tests and integration tests.

The language can be confusing, and different cultures/communities sometimes call these tests with different words. What needs to be done in testing is, starting testing with the smallest meaningful unit possible, and work from there to whole system.(This can be done in reverse order too, but it's out of the scope of this answer)

Related Topic