Home > OS >  How to assert 2 data frames using python pytest
How to assert 2 data frames using python pytest

Time:12-08

I am new to pytest and dataframe. Any help here is appreciated. I have 2 data frames. First dataframe is fetching from csv file and second dataframe is fetching from database.I need to assert all the rows from df1 to compare to df2. Expected result here for df2 to fail as Name "James" is not equal "Linda". I am looking for pytest assertions. Thanks in advance.

Dataframe1: | ID | Name | DOB | Phone| |:---- |:------: | -----:|-----:| | 1 | James | 1/09/2000|0101010| | 2 | Sam | 9/01/1989|0202020|

Dataframe2: | ID | Name | DOB | Phone| |:---- |:------: | -----:|-----:| | 1 | Linda | 1/09/2000|0101010| | 2 | Sam | 9/01/1989|0202020|

Source code:

    from sqlalchemy import create_engine
    import pymysql
    import pandas as pd
    df1 = pd.read_csv(r'Filename.csv')
    sqlEngine = create_engine('mysql pymysql://root:root@localhost', pool_recycle=3600)
    dbConnection = sqlEngine.connect()
    df2 = pd.read_sql("SELECT * FROM tablename", dbConnection);
     dbConnection.close()
    print(df1)
    print(df2)
    def test_compare_database():
    for a, b in zip(df1, df2):
        yield a, b
    @pytest.mark.parametrize('a, b', test_compare_database())
   def test_compare_src_trg_data(a, b):
    assert a == b

Result - When I run the above code, only first row of both dataframe is compared.

Passed tests/main_test.py::test_compare_src_trg_data[ID-ID] 0.00
Passed tests/main_test.py::test_compare_src_trg_data[Name-Name] 0.00
Passed tests/main_test.py::test_compare_src_trg_data[DOB-DOB] 0.00
Passed tests/main_test.py::test_compare_src_trg_data[Phone-Phone] 0.00

CodePudding user response:

You can use the following function:

import pandas as pd
pd.testing.assert_frame_equal(a,b)
  • Related