Functional Testing vs Unit Testing

When it comes to testing there are two different ways of performing such automated tests: functional testing and unit testing

When doing unit testing you will be testing your app from a programmer’s perspective, on the other hand, when doing functional testing, you will be testing your app from your user’s perspective.

Unit Testing Example

import json
import urllib
from pprint import pprint
from urllib import request, parse
import pytest

from inspect import getsourcefile
from os import path, sys
import sys

import sqlalchemy
from sqlalchemy.exc import SQLAlchemyError

current_dir = path.dirname(path.abspath(getsourcefile(lambda:0)))
sys.path.insert(0, current_dir[:current_dir.rfind(path.sep)])

from theroot.users_bundle.models.user import User
from theroot.users_bundle.controllers import do_the_signup
from theroot.services import *
from config import *
sys.path.pop(0)

from theroot import create_app


class TestApp(object):
    @pytest.fixture
    def app(self):
        myapp = create_app()
        myapp.config.from_object('config.TestingConfig')
        return myapp

    def test_create_user(self, client):
        values = {
            "data": {
                "email": "ziocaro02@gmail.com",
                "password": "test",
                "first_name": "ciccio",
                "last_name": "pizzo",
                "role": "1",
                "address": {
                    "country": "Italy",
                    "address_line": "Test Address, 1998, WXDJI Neverland, Italy",
                    "coordinates": {
                        "lat": "41.0914808",
                        "long": "16.8672337"
                    }
                }
            }

        }

        do_the_signup(values)
        rows = db.session.query(User.email).filter_by(email=values['data']['email']).all()
        results = [row for (row,) in rows]
        assert len(results) == 1

Functional Testing Example

 class TestApp(object):
    TEST_URL = 'http://0.0.0.0:5001'

    def test_create_user(self):

        values = {
            "data": {
                "email": "cicciopizzo88@gmail.com",
                "password": "test",
                "first_name": "ciccio",
                "last_name": "pizzo",
                "role": "1",
                "address": {
                    "country": "Italy",
                    "address_line": "Test Address, 1998, WXDJI Neverland, Italy",
                    "coordinates": {
                        "lat": "41.0914808",
                        "long": "16.8672337"
                    }
                }
            }

        }

        headers = {
            "Content-Type": "application/json"
        }
        data = json.dumps(values).encode('utf8')
        url = TestApp.TEST_URL + '/api/user'
        req = urllib.request.Request(url, data=data, headers=headers)

        with urllib.request.urlopen(req) as response:
            the_page = response.read()
            the_page = json.loads(the_page)
            assert the_page['status'] == "success"

In the first example, our insertion and then extraction from the database of the user’s e-mail was performed using SQL Alchemy ORM, in the second case, by using urllib we simulated the call cast by a browser to a specific route.

You can find the source code here

Related Post