簡體   English   中英

pyfakefs fixture 導致 pandas.read_csv() 在 pytest 中失敗

[英]pyfakefs fixture causes pandas.read_csv() to fail in pytest

我正在使用pandas read_csv() function 讀取一些 CSV 內容,並希望使用“高”或“round_trip”浮點精度。

以下工作在 Python REPL 中或直接使用 python 解釋器運行程序。

import pandas as pd
from io import StringIO

df = pd.read_csv( StringIO('0.00042119645,3.4,8.8244e-5\r\n'), float_precision='round_trip' )

如果我輸入pytest測試它也有效:)

但是,如果我使用pyfakes fixture 那么它就會失敗! 出於某種原因, pd.read_csv() function 使用python引擎而不是默認的C引擎。 即使我明確設置engine='c'也是如此。

報錯是:

ValueError("The 'float_precision' option is not supported with the 'python' engine")
import pandas as pd
from io import StringIO

def test_pandas_read_csv(
        #mocker,     #! pytest-mock test fixture
        fs,         #! pyfakefs test fixture
        ):
    try :
        df = pd.read_csv( StringIO('0.00042119645,3.4,8.8244e-5\r\n'), float_precision='round_trip' )
        assert True
    except Exception as exc:
        assert False

如何在我的pytest測試中使用默認c引擎的 pandas read_csv() function 也需要pyfakes夾具?

這是我的 pytest 代碼。 測試失敗 - 注釋掉fs,行以使其通過所有測試。

import pytest

import pandas as pd
from io import StringIO

class Test_Pandas_Read_CSV :

    @pytest.mark.parametrize(
            'csv_str,kwargs,exp_status',
            [
                (
                    ('0.00042119645,3.4,8.8244e-5\r\n'),    #! csv_str
                    dict(                                   #! kwargs
                        float_precision='round_trip',
                        # engine='c',
                    ),
                    True,                                   #! exp_status
                ),
                (
                    ('0.00042119645,3.4,8.8244e-5\r\n'),    #! csv_str
                    dict(                                   #! kwargs
                        float_precision='round_trip',
                        engine='c',
                    ),
                    True,                                   #! exp_status
                ),
                (
                    ('0.00042119645,3.4,8.8244e-5\r\n'),    #! csv_str
                    dict(                                   #! kwargs
                        float_precision='round_trip',
                        engine='python',
                    ),
                    False,                                  #! exp_status
                ),
            ]
        )
    def test_pandas_read_csv(
            self,
            # mocker,                                         #! pytest-mock test fixture
            fs,                                             #! pyfakefs test fixture
            csv_str     : str,
            kwargs,
            exp_status  : bool,
        ) :

        try :
            df = pd.read_csv( StringIO(csv_str), **kwargs )
            status = True
        except Exception as exc:
            status = False

        assert status == exp_status

My work-around involves mocking the pandas.read_csv() call with a function that first pops the float_precision key from the keyword arguments, and then calls the proper/original pandas.read_csv() function. I wrapped that in a customized pyfakes fixture to做 mocking

雖然這使測試通過,但我擔心沒有調用真正的基於 C 的函數,因此結果可能與在真實目標上運行的結果不同。

orig_pandas_read_csv = pd.read_csv

def mock_pandas_read_csv( *args, **kwargs ):
    kwargs.pop('float_precision', None)
    return orig_pandas_read_csv( *args, **kwargs )


@pytest.fixture
def my_fs(mocker, fs):
    mocker.patch.object( pd, 'read_csv', new=mock_pandas_read_csv )
    yield fs

...

    def test_pandas_read_csv(
            self,
            # mocker,                                         #! pytest-mock test fixture
            my_fs,                                          #! pyfakefs test fixture (with mocked pandas functions)
            csv_str     : str,
            kwargs,
            exp_status  : bool,
            ) :
        try :
            df = pd.read_csv( StringIO(csv_str), **kwargs )
            status = True
        except Exception as exc:
            status = False

        assert status == exp_status

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM