【问题标题】:unittest raise exception pythonunittest引发异常python
【发布时间】:2018-08-23 16:30:28
【问题描述】:

我有以下内容来查看 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)

我得到一个全局名称 assertTrue 未定义。 workitem_df 只是我正在传递的一个空框架。有什么我遗漏的或更好的方法吗?

【问题讨论】:

标签: python unit-testing exception pytest


【解决方案1】:

此错误的意思是:assertTrue 未定义。你导入assertTrue了吗?如果你导入了测试模块而不是框架,你必须这样做:

testing_module.assertTrue()

如果你想直接使用'assertTrue`,你应该在测试文件的顶部写下:

from testing_module import assertTrue

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多