Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


PyTest + Mocks

23 Nov 2022 »
pip3 install pytest
pytest #run

app.py

def lol():
    return "lol"

def extract(cluster_name):
    return 0

def transform(data):
    return data + 1

test_app.py

import app
import pytest
from unittest.mock import Mock

def mock_extract(cname):
    print("cname", cname)
    if cname == "A":
        return 1
    elif cname == "B":
        return 2

@pytest.fixture
def myapp():
    myapp = app
    myapp.extract = Mock(side_effect=mock_extract)
    return myapp

def test_lol(myapp):
    r = myapp.lol()
    print("r", r)
    assert r == "lol" 

def test_clusterA(myapp):
    cname = "A"
    data = myapp.extract(cname)
    datab =  myapp.transform(data)
    print(datab)
    assert datab == 2