我正在编写一些单元测试,其中之一是检查数据帧中提供的数据是否为正确的类型(浮点型)。

当我运行测试assertIsInstance(type(foo), np.float64)时,测试失败并显示以下错误消息:AssertionError <class numpy.float64> is not an instance of <class numpy.float64>

我原以为这会过去。

test_dataframe_functions.py

import numpy as np
from django.test import TestCase
import pandas as pd
from myapp import dataframe_functions

class DataframeFunctionsTest(TestCase):
    dataframe_to_test = dataframe_functions.create_dataframe

    #passes
    def test_dataframe_is_definitely_a_dataframe(self):
        self.assertIsInstance(self.dataframe_to_test, pd.DataFrame)

    #passes
    def test_dataframe_has_the_right_columns(self):
        column_headers = list(self.dataframe_to_test.columns)
        self.assertEquals(column_headers, ['header1', 'header2', 'header3'])

    #fails with AssertionError <class numpy.float64> is not an instance of <class numpy.float64>
    def test_dataframe_header1_is_correct_format(self):
        data_instance = self.dataframe_to_test['header1'].iloc[1]
        self.assertIsInstance(type(data_instance), np.float64)


我检查了type(data_instance)是否使用以下代码行等于“ class numpy.float64”:

print(type(dataframe_to_test['header1'].iloc[1]))

, np.float64)

关于python - 为什么在比较numpy.float64的两个实例时django Unittest会引发断言错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58900213/

10-13 09:43