在代码中保留Trace.Assert并且Debug.Assert语句是"稳定的"并且已经转移到测试和生产环境中是否明智?
如果是这样,这些断言语句如何帮助?是否足以让Guard类等检查异常情况并根据需要引发异常?
我正在开发一个多线程Java程序,在整个代码中使用不同的断言,并使用ea标志运行我的程序.
当任何断言失败时,我可以立即停止并退出程序吗?
public class Foo
{
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
public override int GetHashCode()
{
var hash = 17;
hash *= 23 + x.GetHashCode();
hash *= 23 + y.GetHashCode();
hash *= 23 + z.GetHashCode();
}
}
Run Code Online (Sandbox Code Playgroud)
当你去单元测试GetHashCode时,我在计算原始组件和重复函数或使用预定值之间徘徊:
[TestMethod]
public void Test1
{
var x = 1; y = 2; z = 3;
var foo = new Foo() { X = x, Y = y, Z …Run Code Online (Sandbox Code Playgroud) 在我的一个JUnit测试中,我正在初始化一个对象:
MyObject myObject = new MyObject(220, 120, 0.05, true);
Run Code Online (Sandbox Code Playgroud)
构造函数的签名是:
public MyObject(int minLength, int maxLength,
double onBitsRatio, boolean forceAtLeastOneBitOn)
Run Code Online (Sandbox Code Playgroud)
其次是:
assert(onBitsRatio >= 0.0 && onBitsRatio <= 1.0);
assert(maxLength>=minLength);
assert(false);
Run Code Online (Sandbox Code Playgroud)
奇怪的是,断言不会像我期望的那样停止执行.
为什么JUnit会忽略这些断言?
简单地说,Debug.Assert失败的调用不会停止MonoDevelop中程序的执行流程,它们只是打印出一个像这样开始的跟踪消息(后跟堆栈跟踪):
2012-12-28 19:21:01.978 TestApp[81689:c07] ---- DEBUG ASSERTION FAILED ----
2012-12-28 19:21:01.979 TestApp[81689:c07] ---- Assert Short Message ----
Run Code Online (Sandbox Code Playgroud)
我可以做些什么来强制在MonoDevelop中Debug.Asserts破坏执行失败Assert?
是否内置支持返回结果的断言?
这样做是非常不起作用的:
def addPositive(a: Int, b: Int) = {
assert(a > 0 && b > 0)
a + b
}
Run Code Online (Sandbox Code Playgroud)
我宁愿做类似的事情:
def addPositive(a: Int, b: Int) =
assert(a > 0 && b > 0)(a + b)
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我可以避免断言的必要方面.(后者不编译)有类似的可用吗?
在试图理解assertPython时,特别是反转它,我想出了这个......
>>> assert != ( 5 > 2 )
>>> assert != ( 2 > 5 )
Run Code Online (Sandbox Code Playgroud)
现在第一行失败,第二行失败.什么是断言某事的惯用方法是错误的?
我使用带有disableassertions的JAVA_OPTS,但是当gradle test运行时,仍然有输出java.lang.AssertionError.为什么?
build.gradle:
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: "groovy"
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.6' // for compile groovy
compile "org.springframework:spring-core:3.0.5.RELEASE"
compile "org.springframework:spring-aop:3.0.5.RELEASE"
compile "org.springframework:spring-asm:3.0.5.RELEASE"
compile "org.springframework:spring-beans:3.0.5.RELEASE"
compile "org.springframework:spring-context:3.0.5.RELEASE"
compile "org.springframework:spring-expression:3.0.5.RELEASE"
compile "org.springframework:spring-jdbc:3.0.5.RELEASE"
compile "org.springframework:spring-orm:3.0.5.RELEASE"
compile "org.springframework:spring-test:3.0.5.RELEASE"
compile "junit:junit:4.+"
}
Run Code Online (Sandbox Code Playgroud)
gradle test 产量
:booking:processResources UP-TO-DATE
:booking:classes
:booking:jar
:compileJava
:compileGroovy
:processResources UP-TO-DATE
:classes
:compileTestJava UP-TO-DATE
:compileTestGroovy
:processTestResources UP-TO-DATE
:testClasses
:test
ScriptTester > testHandle FAILED
java.lang.AssertionError at ScriptTester.groovy:127
Run Code Online (Sandbox Code Playgroud) 如何用chai断言某个div('。MyDive')包含文本'Some text'?
<div class="MyDive">
<div>Some text</div>
</div>
Run Code Online (Sandbox Code Playgroud) 我想做这样的事情:
TEST(MyTestFixture, printAfterExpectationFailure)
{
const string request("bring me tea");
const string&& response = sendRequestAndGetResponse(request);
checkResponseWithExpectarions1(response);
checkResponseWithExpectarions2(response);
checkResponseWithExpectarions3(response);
checkResponseWithExpectarions4(response);
if (anyOfExpectsFailed())
cout << "Request: " << request << "\nresponse: " << response << endl;
}
TEST(MyTestFixture, printAfterAssertionFailure)
{
const string request("bring me tea");
const string&& response = sendRequestAndGetResponse(request);
doWhenFailure([&request, &response]()
{
cout << "Request: " << request << "\nresponse: " << response << endl;
});
checkResponseWithAssertion1(response);
checkResponseWithAssertion2(response);
checkResponseWithAssertion3(response);
checkResponseWithAssertion4(response);
}
Run Code Online (Sandbox Code Playgroud)
我仅在期望/断言失败时才打印一些其他信息。
我知道我可以做这样的事情:
#define MY_ASSERT_EQ(lhr, rhs, message) if(lhs != rhs) ASSERT_EQ(lhs, rhs) …Run Code Online (Sandbox Code Playgroud) assert ×10
c# ×2
java ×2
.net ×1
assertions ×1
c++ ×1
chai ×1
debugging ×1
eclipse ×1
expectations ×1
gethashcode ×1
googletest ×1
gradle ×1
junit ×1
mono ×1
monodevelop ×1
python ×1
scala ×1
unit-testing ×1