Search code examples
pythontestingassert

Test python function


Good morning! I need to create a test for a python function. The result to be tested is a very large dataframe in json format. What can I put in assert to test that the json file has been created correctly? Thank you

def function (param):
   ...
   ...
assert ()

Solution

  • Hi depends on the test you are searching for.

    Approach I (functional)

    The returned json have a structure, so you can verify for example that the number of columns are correct, the number of rows are correct and so on:

    jdf = DataFrame({'A':[1,2], 'B':[3,4]}).to_json()
    assert len(json.loads(jdf)) ==2
    assert set(json.loads(jdf).keys()) == {'A','B'}
    

    Approach II (non regression)

    If you have a previous verified df stored elsewhere you can check that all values are as expected using the assert_frame_equal