我有一个测试方法:
[TestMethod()]
public void test_chars()
{
MyBO target = new MyBO() { x = 'S' };
char[] expected = {'D','d','M','m','L','l'};
char actual = target.x;
Assert.AreEqual(actual, expected); // ?
}
Run Code Online (Sandbox Code Playgroud)
我如何检查Assert.AreEqual是否target.x在那char[] expected?因此,如果'S'不是该数组的一部分,则测试应该失败.这可能吗?
机器人评判吮吸!只有在删除以下代码块后,我才被UVA接受:
cin >> ntc;
/* extract newline from previous read */
char dummy(0);
cin.get(dummy);
assert( '\n'==dummy );
/* newline extract completes */
Run Code Online (Sandbox Code Playgroud)
替换为:
cin >> ntc;
getline( cin, inputN ); /* extract remaining '\n' */
Run Code Online (Sandbox Code Playgroud)
在更换之前,UVA的光荣机器人法官会做出判决:
您的提交....已失败,判决超出时限.
您的程序使用的CPU时间超过了此问题所允许的时间.这意味着您的算法速度不够快或进入无限循环.
更换后程序需要0.052秒才能运行!
我用MinGW.
我正在学习Clojure并且正在乱搞REPL.因为assert,文件说
Usage: (assert x)
(assert x message)
Evaluates expr and throws an exception if it does not evaluate to
logical true.
Run Code Online (Sandbox Code Playgroud)
所以我期望(assert false "Error message")引发错误和输出"Error message".但是,它给出了
java.lang.IllegalArgumentException: Wrong number of args (4) passed to: core$assert (NO_SOURCE_FILE:0)
Run Code Online (Sandbox Code Playgroud)
为什么它说我有四个参数,什么是正确的用法?文档assert不是太冗长.
我编写了一个简单的测试程序来测试__debug__使用globals()["__debug__"] = value(__debug__ = value是SyntaxError)分配功能。它基本上会尝试引发an AssertionError并打印是否引发错误以及是否预期错误。当我遇到__debug__更改中间程序的问题时,我就是这样做的。
print("__debug__ is", __debug__)
exp = ["(expected)", "(not expected)"]
if __debug__:
exp = exp[::-1]
try:
assert False
print("No AssertionError", exp[0])
except AssertionError:
print("AssertionError", exp[1])
exp = exp[::-1]
globals()["__debug__"] = not __debug__
print("__debug__ is", __debug__)
try:
assert False
print("No AssertionError", exp[0])
except AssertionError:
print("AssertionError", exp[1])
Run Code Online (Sandbox Code Playgroud)
从命令提示符运行时,无论是否带有该-O标志,它都会产生意外的结果。
C:\Test>python assert.py
__debug__ is True
AssertionError (expected)
__debug__ is False
AssertionError (not expected)
C:\Test>python -O assert.py
__debug__ …Run Code Online (Sandbox Code Playgroud) 我有一个奇怪的问题,Asserts被忽略了.您可以使用此最小示例重现它.我想知道为什么会出现这个问题以及如何面对它:
public class TestAssert
{
public string EmptyString
{
get
{
System.Diagnostics.Debug.Assert(false);
return string.Empty;
}
}
Dictionary<string, object> dict = new Dictionary<string, object>();
public void ShowAssertIgnored()
{
var foo = dict[EmptyString];
}
}
Run Code Online (Sandbox Code Playgroud)
Debug.Assert(false)即使评估了属性,您也可以看到它被忽略.打电话吧
var test = new TestAssert();
test.ShowAssertIgnored();
Run Code Online (Sandbox Code Playgroud)
代码编译并在Debug中运行(其他断言工作正常!),32位,x86 + AnyCPU,VS2012专业,.Net Framework 4
编辑:
该项目是一个控制台应用程序,我连续几次运行它.在System.Diagnostics.Debug.Assert(false);最常见的断点之前会出现一个消息框.但并非总是如此:当我只是多次重试相同的情况时,我有时会在控制台中看到结果.
再说一遍:我可以在VS2012调试器中重现非确定性行为!
我已经阅读了一些内容,试图弄清楚何时适当地使用断言和异常,但是我仍然缺少大局.可能我只需要更多的经验,所以我想带一些简单的例子来更好地理解我应该使用什么情况.
示例1:让我们从无效值的经典情况开始.例如,我有以下类,其中两个字段必须为正数:
class Rectangle{
private int height;
private int length;
public int Height{
get => height;
set{
//avoid to put negative heights
}
}
//same thing for length
}
Run Code Online (Sandbox Code Playgroud)
让我说一下,我不是在讨论如何在这个例子中处理用户输入,因为我可以为此做一个简单的控制流程.虽然,我面临的想法是,在其他地方可能会出现一些意外错误,我希望能够检测到这一点,因为我不想要一个带有invald值的对象.所以我可以:
Debug.Assert如果发生这种情况,请使用并停止程序,这样我就可以纠正可能出现的错误.ArgumentOutOfRangeException基本上做同样的事情?这感觉不对,所以只有当我知道我要在某个地方处理它时我才应该使用它.虽然,如果我知道在哪里处理异常,我不应该解决它所在的问题吗?或者它可能意味着可能发生的事情,但你不能直接在你的代码中控制,比如用户输入(可以处理,没有例外,但可能是别的东西不能)或加载数据?问题:我是否明白了断言和例外的含义?另外,请举例说明处理异常是否有用(因为之前你无法控制的东西)?除了我提到的情况之外,我无法弄清楚还有什么可以发生,但我显然仍然缺乏经验.
为了扩展我的问题:我可以想出为什么可以抛出异常的各种原因,比如a NullReferenceException,a IndexOutOfBoundsException,IO异常,DirectoryNotFoundException或者FileNotFoundException等等.虽然,我无法弄清楚处理它们变得有用的情况,分开从简单地停止程序(在这种情况下,不应该使用断言?)或给出一个简单的消息,问题发生在哪里.我知道即使这是有用的,例外也意味着对"错误"进行分类并提供如何解决它们的线索.虽然,这是一个简单的消息,它们真的对它们有用吗?这听起来很可疑,所以我会坚持"我从来没有遇到过适当的情况,"经验的原因"口头禅.
示例2:现在让我们使用第一个示例来讨论用户输入.正如我所预料的那样,我不会仅仅使用例外来检查值是否为正,因为这是一个简单的控制流程.但是如果用户输入一个字母会发生什么?我应该在这里处理一个例外(可能很简单ArgumentException)并在catch块中给出一条消息吗?或者也可以通过控制流程来检查(检查输入是否类型int,或类似的东西)?
感谢任何能够消除我挥之不去的怀疑的人.
我正在尝试使用testNG执行我的测试脚本并尝试下面的代码但是0显示反对运行,失败并在控制台中跳过...因为我无法在我的脚本中验证结果
package com.demoaut.newtours.testcases;
import org.testng.Assert;
import org.testng.annotations.Test;
//import junit.framework.Assert;
public class TC002_CheckAssert {
@Test
public TC002_CheckAssert() {
System.out.println("ajkcbh");
try {
Assert.assertEquals("Pass", "Pass");
}
catch(Exception e) {
System.out.println("Exception:" + e.getLocalizedMessage());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我通过testng.xml文件执行上面的脚本
<suite name="Suite">
<test name="Test">
<classes>
<class name="com.demoaut.newtours.testcases.TC002_CheckAssert" />
</classes>
</test>
</suite>
Run Code Online (Sandbox Code Playgroud)
控制台resule
ajkcbh
"==============================================="
套房
总测试运行:0,失败:0,跳过:0
"==============================================="
我已经编写了一些测试,我需要声明两个数组相等.有些阵列[u8; 48]而有些则是[u8; 188]:
#[test]
fn mul() {
let mut t1: [u8; 48] = [0; 48];
let t2: [u8; 48] = [0; 48];
// some computation goes here.
assert_eq!(t1, t2, "\nExpected\n{:?}\nfound\n{:?}", t2, t1);
}
Run Code Online (Sandbox Code Playgroud)
我在这里收到多个错误:
error[E0369]: binary operation `==` cannot be applied to type `[u8; 48]`
--> src/main.rs:8:5
|
8 | assert_eq!(t1, t2, "\nExpected\n{:?}\nfound\n{:?}", t2, t1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `[u8; 48]`
= note: this error originates in …Run Code Online (Sandbox Code Playgroud) 有没有办法在一个方法中断言输入String有一定的长度?
我试过断言,stringName[4];但似乎不起作用
我正在使用请求模块和python assert关键字设置请求断言,但是
AttributeError:'dict'对象没有属性'documentation_url'
当我尝试在json响应中声明字符串时。我如何在json响应中声明某些内容,当条件为true时,它应该打印出某些内容?
import requests
import pprint
URL = 'https://github.com/timeline.json'
def get_github_json(URL):
response = requests.get(URL).json()
return response
assert get_github_json(URL).documentation_url == 'https://developer.github.com/v3/activity/events/#list-public-events'
Run Code Online (Sandbox Code Playgroud)
json响应如下所示:
{'documentation_url': 'https://developer.github.com/v3/activity/events/#list-public-events',
'message': 'Hello there, wayfaring stranger. If you’re reading this then you '
'probably didn’t see our blog post a couple of years back '
'announcing that this API would go away: http://git.io/17AROg Fear '
'not, you should be able to get what you need from the shiny new '
'Events API instead.'
}
Run Code Online (Sandbox Code Playgroud)