unittest引发异常python

时间:2018-08-23 16:30:29

标签: python unit-testing exception pytest

我要检查一下DataFrame是否为空:

def emptyframecheck(data_frame):
if data_frame.empty:
    raise Exception('An empty dataframe was passed.')

我想对此进行单元测试,但是当我运行它时:

def test_emptyframecheck():
    # Given
    workitem_df = pd.read_csv(os.path.join(HERE, 'test_csv', 'test.csv'))

    # When
    emptyframecheck(workitem_df)

    # Then
    assertTrue('An empty dataframe was passed' in emptyframecheck.exception)

我没有定义全局名称assertTrueworkitem_df只是我正在传递的空白帧。有什么我想念的还是更好的方法?

1 个答案:

答案 0 :(得分:1)

此错误表示其含义:assertTrue未定义。您导入了assertTrue吗?如果您导入测试模块而不是框架,则必须执行以下操作:

testing_module.assertTrue()

如果您想直接使用'assertTrue',则应在测试文件的顶部编写以下内容:

from testing_module import assertTrue