加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – unittest.TestCase.assertEqual的参数顺序

发布时间:2020-12-16 22:46:42 所属栏目:Python 来源:网络整理
导读:是assertEqual(actual,expected)还是assertEqual(expected,actual)? 一方面,我看到很多代码使用assertEqual(actual,expected).这包括examples in the unittest docs和examples in the Django docs. 但是,这个测试assertEqual(foo,bar)给了我输出 - foo+ bar

是assertEqual(actual,expected)还是assertEqual(expected,actual)?

一方面,我看到很多代码使用assertEqual(actual,expected).这包括examples in the unittest docs和examples in the Django docs.

但是,这个测试assertEqual(‘foo’,’bar’)给了我输出

- foo
+ bar

这恰好与使用assertEquals(‘foo’,’bar’)的PHPUnit测试相同;

-'foo'
+'bar'

并且PHPUnit将$expected作为第一个参数,然后是$actual.这种差异也是我期望的,实际的.

所有这些Python代码我看到做错了吗?

我检查了unittest方法的定义,尽管它有非常有用的第一个,第二个参数名称.

最佳答案
根据assertEqual文件:

Test that first and second are equal. If the values do not compare equal,the test will fail.

因此,您可以将预期和实际放在任何地方,它将返回相同的结果.但通常的做法是将实际结果用作第一个参数,将预期结果用作第二个参数.它也在Python 3’s Unittest Example中得到证明:

s = 'hello world'
self.assertEqual(s.split(),['hello','world'])

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读