less than 1 minute read

I talked about co-locating code and tests before in my blog called Should you colocate your tests? A proof-of-concept.

What if we take this concept even a step further and put the code and tests in the same file?!

Something like this:

import pytest

test_add_table = [
    (0,0,0),
    (1,1,2),
    (3,4,7),
    pytest.param("0", 1, 1, marks=pytest.mark.xfail)
]
@pytest.mark.parametrize("a,b,res", test_add_table)
def test_add(a,b,res):
    assert add(a,b) == res

def add(a: int, b:int) -> int:
    return a + b

if __name__ == "__main__":
    print(add(10, 10))

I think this is a crazy idea and honestly I think it would get very messy, but I’m all out for trying such a crazy idea once just to better understand why it wouldn’t work.

Find the full code on Github here.

Subscribe

Comments