我目前正在尝试学习如何使用单元测试,并且我已经创建了 3 个动物对象的实际列表和 3 个动物对象的预期列表。问题是我如何断言检查列表是否相等?我试过 CollectionAssert.AreEqual 和 Assert.AreEqual 但无济于事。任何帮助,将不胜感激。
测试方法:
[TestMethod]
public void createAnimalsTest2()
{
animalHandler animalHandler = new animalHandler();
// arrange
List<Animal> expected = new List<Animal>();
Animal dog = new Dog("",0);
Animal cat = new Cat("",0);
Animal mouse = new Mouse("",0);
expected.Add(dog);
expected.Add(cat);
expected.Add(mouse);
//actual
List<Animal> actual = animalHandler.createAnimals("","","",0,0,0);
//assert
//this is the line that does not evaluate as true
Assert.Equals(expected ,actual);
}
Run Code Online (Sandbox Code Playgroud) 我有一个返回 Excel 文件的操作。在浏览器中访问该路线将自动下载我的 Excel 文件。
我为该操作编写了一个测试,但我只能断言来自标题的数据。此外,测试将响应直接输出到控制台,并带有类似w?m?J?9???9?e??o??.
有没有办法对来自该 Excel 文件输出的数据进行断言?不保存文件,只显示内容作为响应。我正在使用 Symfony2。
编辑
我找到了一种通过将响应保存在输出缓冲区中、将内容保存在 .xlsx 文件中、转换为 .csv 文件并从那里获取内容来读取数据的方法。
ob_start();
$this->client->request(
'GET',
'/myUrl',
array(),
array(),
array()
);
$content = ob_get_clean();
file_put_contents('MyFile.xlsx', $content);
$reader = \PHPExcel_IOFactory::createReader('Excel2007');
$excel = $reader->load('MyFile.xlsx');
$writer = \PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->save('MyFileCsv.csv');
$newContent = fgetcsv(fopen("MyFileCsv.csv", "r"));
Run Code Online (Sandbox Code Playgroud)
这里只是一个草图。我停在这里,没有尝试阅读所有内容并进行断言,但我确信可以通过这种方式完成。啊,别忘了之后删除文件。
我想对函数进行单元测试并断言是否在函数工作流()内部进行的函数调用序列。就像是,
[1st called] fetch_yeargroup_ls()
[2nd called] invoke_get_links().......
Run Code Online (Sandbox Code Playgroud)
我搜索了许多讨论,但从未找到一个能回答我的问题。
我在一篇文章中读到,放置以下预处理器行会忽略所有后续的 assert 预处理器指令。但这似乎不起作用,并且断言语句实际上由预处理器处理并在断言中的条件不满足时中止程序(我知道当断言条件不满足时中止是正确的行为)。我的问题是为什么通过放置#define NDEBUG 不会忽略断言语句。代码是
#include<stdio.h>
#include<assert.h>
#define NDEBUG
int main(){
int x = 0;
assert (x!=0);
}
Run Code Online (Sandbox Code Playgroud) 我在 node.js https://www.npmjs.com/package/assert 中使用 assert 模块
在 C/C++ 中,可以使用宏轻松启用/禁用断言语句。通常可以对 node.js 和 javascript 做同样的事情吗?
今天我有一个失败的测试,很高兴成功了,因为我在最后忘记了一个相当重要的行:
assert actual == expected
Run Code Online (Sandbox Code Playgroud)
我希望机器将来能捕捉到这个错误。有没有办法让 pytest 检测测试函数是否没有断言任何内容,并将其视为测试失败?
当然,这需要一个“全局”的配置设置;注释每个测试函数@fail_if_nothing_is_asserted会破坏目的。
我读了这个关于何时使用断言的答案,基本上是说使用它来进行在生产中关闭的测试:适当使用断言
(Oracle 文档说As an added benefit, assertions serve to document the inner workings of your program, enhancing maintainability.这是有道理的,但我不清楚为什么它是额外的好处而不是唯一的好处。)
我的想法是:
我有什么误解?预先感谢您的帮助 :)
阅读硬件总线规范时,我经常遇到诸如:“当控制器看到已发出总线请求时,它会断言总线授予线到第一个设备。”
“断言”一词在这里究竟是什么意思?它像“激活”一样简单,还是还有更多?
Pytest 对失败断言的输出比 Python 中的默认值提供更多信息和有用。我想在正常运行我的 Python 程序时利用它,而不仅仅是在执行测试时。有没有办法从我的脚本中覆盖 Python 的assert行为,以使用 pytest 打印堆栈跟踪,同时仍将我的程序作为python script/pytest_assert.py?
def test_foo():
foo = 12
bar = 42
assert foo == bar
if __name__ == '__main__':
test_foo()
Run Code Online (Sandbox Code Playgroud)
$ python script/pytest_assert.py
Traceback (most recent call last):
File "script/pytest_assert.py", line 8, in <module>
test_foo()
File "script/pytest_assert.py", line 4, in test_foo
assert foo == bar
AssertionError
Run Code Online (Sandbox Code Playgroud)
$ pytest script/pytest_assert.py
======================== test session starts ========================
platform linux -- Python 3.5.3, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /usr/local/google/home/danijar, inifile:
collected 1 …Run Code Online (Sandbox Code Playgroud) 我有一个这样的字符串
String xyz ="JAVAxxxxxxxxxxxxxx ID: 123678 VERSION: 3"
Run Code Online (Sandbox Code Playgroud)
我想知道如何验证它是否符合特定模式。
我做这样的断言
assertThat(xyz).containsPattern("[A-Za-z]* ID: [0-9]* VERSION: [0-9]* ");
Run Code Online (Sandbox Code Playgroud)