Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting asserting the value None error when unit testing with Pytest

I'm trying to do a very simple assertion of equality of my expected dataframe output to the result dataframe once the test data is run through the test function. However, instead of comparing if they're are the same, I'm getting None.

I don't have this issue with any of my other tests that use dataframes. I also can print out the dataframe for both the expected AND the result so I know they both exists. Finally, I can even run full production data through the function and it comes out as expected.

I figure I'm missing something but I'm not sure what. I haven't really tried anything other than playing around with it. I assume it's something to do with how dataframes are structured.

FUNCTIONS

def get_valid_postal_code(postal_code):
    valid_pc = len(postal_code) == 6 and postal_code[0:5:2].isalpha() and postal_code[1:6:2].isnumeric()
    return valid_pc

def clean_postal_codes(sales):
    sales = sales.dropna(subset=['Postal_Code']).copy()
    sales['Postal_Code'] = sales['Postal_Code'].str.replace(" ", "")
    sales = sales[sales['Postal_Code'].apply(get_valid_postal_code)]
    sales['Postal_Code'] = [x.upper() for x in sales['Postal_Code']]
    return sales

TEST FUNCTION

def test_clean_postal_codes():
    test_data = pd.DataFrame(
        [[344, 'a0a 0a0', 3000], [344, 'a0a 0a0', -2000], [484, 'A0A 0a0', 1000], [494, 'A0a 0A0', 1000],
         [700, 'A0A0A0', 1000], [900, 'A0A0A0', 1000], [50, 'Not null', 2352], [600, 'asdffa', 23523],
         [634, '3HN3H3', 8839], [32, '3523', 238], [432, 'M5N22H', 2352]],
        columns=['Store_Num', 'Postal_Code', 'Sales']
    )
    expected = pd.DataFrame([[344, 'A0A0A0', 3000], [344, 'A0A0A0', -2000], [484, 'A0A0A0', 1000],
                             [494, 'A0A0A0', 1000], [700, 'A0A0A0', 1000], [900, 'A0A0A0', 1000]],
                            columns=['Store_Num', 'Postal_Code', 'Sales'])

    result = clean_postal_codes(test_data)
    print(expected)  # Is not None
    print(result) # Is also not None 
    assert testing.assert_frame_equal(expected, result) # Getting None

I keep get the following error "PytestAssertRewriteWarning: asserting the value None, please use "assert is None". If you run the and print out the expected and result dataframes, you can see that both dataframes exist and are the same. Despite this, the error persists.

like image 624
ShockDoctor Avatar asked Aug 14 '19 16:08

ShockDoctor


1 Answers

I believe that this error occurs because of the function pandas.testing.assert_frame_equal returns None if frames are equal; if they're unequal it raises AssertionError. Therefore you're actually checking assert None.

So I think you should remove assert operator here and just write

testing.assert_frame_equal(expected, result)

Or write

assert testing.assert_frame_equal(expected, result) is None
like image 160
Viacheslav Z Avatar answered Nov 08 '22 19:11

Viacheslav Z