使用 TestNG's Assert.assertEquals(double expected, double actual, double delta),使用以下数字作为增量的一个好数字(断言失败,因为没有增量)
AssertionError: expected [121.97] but found [121.96999999999998]
Run Code Online (Sandbox Code Playgroud)
我尝试使用1、.01、 和.001,但三个都通过了。我希望保持成功的余量非常小(换句话说,如果“实际数字”(无论如何从我的角度来看)是 121.97 和 121.96,我希望测试失败。谢谢!
如果我在visual studio中为我的C#项目定义了Debug常量,我可以确定将评估断言并在失败时显示消息框.但是什么标志属性使CLR在运行时实际上决定是否评估和显示断言.在定义DEBUG时,断言代码是否不会在IL中结束?或者它是程序集的DebuggableAttribute中的DebuggableAttribute.DebuggingModes标志的关键点?如果是这样,它的枚举值必须存在?这是如何工作的?
有可能告诉xUnit.net Assert.True()在一个测试方法中执行所有操作吗?基本上在我们的一些use/testcases中,所有断言在逻辑上都属于同一个"范围"的测试,我有这样的东西:
[Fact(DisplayName = "Tr-MissImpl")]
public void MissingImplementationTest()
{
// parse export.xml file
var exportXml = Libraries.Utilities.XML.GenericClassDeserializer.DeserializeXmlFile<Libraries.MedTrace.ExportXml>(
ExportXmlFile);
// compare parsed results with expected ones
Assert.True(exportXml.ContainsRequirementKeyWithError("PERS_154163", "E0032A"));
Assert.True(exportXml.ContainsRequirementKeyWithError("PERS_155763", "E0032A"));
Assert.True(exportXml.ContainsRequirementKeyWithError("PERS_155931", "E0032A"));
Assert.True(exportXml.ContainsRequirementKeyWithError("PERS_157145", "E0032A"));
Assert.True(exportXml.ContainsRequirementKeyWithError("s_sw_ers_req_A", "E0032A"));
Assert.True(exportXml.ContainsRequirementKeyWithError("s_sw_ers_req_C", "E0032A"));
Assert.True(exportXml.ContainsRequirementKeyWithError("s_sw_ers_req_D", "E0032A"));
}
Run Code Online (Sandbox Code Playgroud)
现在,如果第一个Assert.True(...)失败,则不执行/检查其他的.我宁愿不将这七个断言分解为单独的方法,因为它们确实在逻辑上属于一起(如果所有七个都通过一起,则TC仅被'完全传递'.
以下测试是否应该抛出异常?在我的电脑上它没有,我想知道这是否是预期的行为.
def a
raise RuntimeError
end
def b
begin
a
rescue RuntimeError
puts "bummer"
end
end
test "assert this" do
assert_raises RuntimeError do
b
end
end
Run Code Online (Sandbox Code Playgroud) 我的代码中有一个断言.就像是:
assert('is_string($var)');
Run Code Online (Sandbox Code Playgroud)
如果我为PHPUnit编写测试导致此断言失败并显示消息, Warning: assert(): Assertion "is_string($var)" failed in /path/to/file.php on line ###
而且,我的测试也失败了.我已经尝试根据文档添加@expectedException PHPUnit_Framework_Error_Warning到docblock ,但这没有帮助.我需要做些什么来使我的测试期望这个断言会失败?
所以我正在学习如何在Django中练习TDD,我遇到了一些小问题.我创建了一个自定义用户对象,该对象以一对一的关系链接到经过身份验证的系统用户.我有以下测试,它练习我的自定义用户类的一部分:
def test_creating_a_user_with_attributes(self):
myuser = Myuser.objects.create_user('Gary', email='me@email.com')
current_time = now()
myuser.birthday = current_time
myuser.save()
first_user = Myuser.objects.all()[0]
self.assertEqual(first_user.birthday, current_time, 'first_user.birthday should be equal to the current_time')
Run Code Online (Sandbox Code Playgroud)
问题是我的测试失败了,我无法立即看到原因.断言失败报告了我提供的消息,我感到困惑,因为我确定生日被设置为现在的值.我最终不得不重构我的断言以使失败的值清楚.
self.assertEqual(first_user.birthday, current_time,
'first_user.birthday ' + str(first_user.birthday) + ' should equal ' + str(current_time))
Run Code Online (Sandbox Code Playgroud)
这表明生日是日期字段而不是日期时间字段.我的问题是,是否存在一些替代形式的断言,它将预期值和实际值转储为失败消息的一部分,或者我是否在某种程度上滥用或误解了API?
我最近在文本生成软件的文本输出上做了很多功能测试,发现自己写了很多
assertTrue(actualString.contains(wantedString));
Run Code Online (Sandbox Code Playgroud)
但是,失败的消息是非描述性的
Expected [true], but was [false]
Run Code Online (Sandbox Code Playgroud)
另一种方法是将自定义失败消息包括为
String failMsg = String.format("Wanted string to contain: %s, Actual string: %s", wantedString, actualString);
assertTrue(failMsg, actualString.contains(wantedString));
Run Code Online (Sandbox Code Playgroud)
但是一直手动执行此操作感觉有点乏味.有没有更好的办法?
我确定我测试的数组是null.甚至做var_dump(array)印刷品array(0) {
}.
但测试$this->assertNull($array);失败了.
相反,当我测试下面的代码时,它进入if条件:
if ($array == null) {
echo "Entered";
} else {
echo "Not Entered";
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么会这样.如果有人知道原因,请解释我.
template <size_t N>
class Foo
{
static_assert(N > 0, "WRONG");
//void Something() = 0; //my original implementation
};
int main() {
Foo<0> *p2 = nullptr; //no error
Foo<0> p; //gives an error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我已分别测试了两条线.初始化p2时不调用static_assert但是它被调用并确实在p上失败.这是有意的吗?(我在gcc,clang和VC上尝试过)
解决方法有哪些?由于我正在使用抽象模板化类,如果断言仅在实例化非指针对象时执行,那将是一场噩梦.我可以使用工厂,但这不是一个合适的解决方案.
在对我的请求的响应中,我得到了一个JSON项目列表
{
"Id": 111,
"Name": "aaa"
},
{
"Id": 222,
"Name": "bbb"
}
Run Code Online (Sandbox Code Playgroud)
我需要声明,在我的回复中,至少有5个项目.使用JSONPath Count时,我只能使用$ ..*来检查确切的值.不幸的是,我不知道返回的确切项目数,只要响应中有超过5项,一切都可以.我可以使用任何JSONPath断言吗?