PHPUnit 是否有一些内置的递归数组比较函数?
我需要做的一些测试需要将已知数组与我从将要运行的函数中获得的结果进行比较。
用于递归比较数组:
- PHPUnit 有内置函数吗?
- 这里有人有一些他们构建的代码可以分享吗?
- 这是我必须自己构建的东西吗?
Some of the testing I will need to do will require comparing a known array with the result I am getting from the functions I will be running.
For comparing arrays recursively:
- Does PHPUnit have an inbuilt function?
- Does someone here have some code they have constructed to share?
- Will this be something I will have to construct on my own?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,确实如此。
assertEquals()
和assertNotEquals()
文档。具体来说:
测试代码:
[编辑]
这是无序 aList 的代码:
此测试失败:
带有消息:
Yes it does.
assertEquals()
andassertNotEquals()
documentation.Specifically:
Test Code:
[EDIT]
Here is the code for out of order aLists:
This test fails:
With message:
@wilmoore
会失败。
@Ben Dauphinee
可能值得一看
assertContains(mixed $needle, array $haystack)
但您必须循环遍历两个数组并将每个元素与另一个数组进行比较,以确保它包含所有必需的元素而不是其他元素,但是这不会考虑包含两个相同元素的数组在这种情况下会通过
它也不会考虑任何进一步的递归,这可能会非常复杂处理。
从长远来看,根据复杂性,实现您自己的断言函数可能会更容易。
@wilmoore
Will fail.
@Ben Dauphinee
It might be worth looking at
assertContains(mixed $needle, array $haystack)
but you would have to loop through both arrays and compare each element with the other array to ensure it contained all the required elements and no others, this however wouldn't account for an array containing two identical elementswould pass in this case
It also doesn't account for any further recursion which would probably be quite complicated to deal with.
Depending on the complexity might just be easier in the long run to implement your own assert function.
assertEqual
方法头如下所示:如果 canonicalize 参数设置为 true,则数组将首先通过
sort()
运行,如果键是任意的并且只有价值观才重要。然而,在查看了数组比较器代码之后,assertEqual 实际上并不关心关联数组的顺序:)它只会在结果数组中查找预期的键,然后比较那些钥匙。
The
assertEqual
method head looks like this:If the canonicalize parameter is set to true the arrays will be run through
sort()
first, this is usable if the keys are arbitrary and only the values matters.However after looking over the array comparator code, the
assertEqual
actually does not care about the order of an associated array :) It will simply look for the expected key in the result array, and then compare the values of those keys.我在使用一些带有键的生成数组时遇到了这个问题 - 在调用assertEquals之前,我最终传递了预期数组和通过ksort测试的数组。但这不适用于递归数组。
I had this problem with some generated arrays with keys - I ended up passing both the expected array and the array being tested through ksort before calling assertEquals. This doesn't work on recursive arrays, though.