标签: assert

.NET生产代码中的"断言"语句

在代码中保留Trace.Assert并且Debug.Assert语句是"稳定的"并且已经转移到测试和生产环境中是否明智?

如果是这样,这些断言语句如何帮助?是否足以让Guard类等检查异常情况并根据需要引发异常?

.net assert

4
推荐指数
1
解决办法
627
查看次数

如果断言失败,则退出java程序

我正在开发一个多线程Java程序,在整个代码中使用不同的断言,并使用ea标志运行我的程序.

当任何断言失败时,我可以立即停止并退出程序吗?

java assert

4
推荐指数
1
解决办法
4568
查看次数

单元测试 - 当你的代码几乎只是一个计算(例如GetHashCode)时,你会怎么做?

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)

c# unit-testing assert gethashcode

4
推荐指数
1
解决办法
1276
查看次数

assert(false)不会停止执行

在我的一个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会忽略这些断言?

java eclipse junit assert

4
推荐指数
2
解决办法
3954
查看次数

Debug.Assert不会破坏MonoDevelop/MonoTouch中的编辑器

简单地说,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?

c# debugging mono assert monodevelop

4
推荐指数
1
解决办法
895
查看次数

Scala中的功能断言

是否内置支持返回结果的断言?

这样做是非常不起作用的:

  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)

通过这种方式,我可以避免断言的必要方面.(后者不编译)有类似的可用吗?

assert functional-programming scala

4
推荐指数
1
解决办法
2070
查看次数

你如何断言某些东西在Python中是不正确的?

在试图理解assertPython时,特别是反转它,我想出了这个......

>>> assert != ( 5 > 2 )
>>> assert != ( 2 > 5 )
Run Code Online (Sandbox Code Playgroud)

现在第一行失败,第二行失败.什么是断言某事的惯用方法是错误的?

python assert

4
推荐指数
1
解决办法
6848
查看次数

如何在gradle测试中禁用断言

我使用带有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)

assert gradle

4
推荐指数
1
解决办法
2116
查看次数

如何用chai断言某个div包含一些文本?

如何用chai断言某个div('。MyDive')包含文本'Some text'?

<div class="MyDive">
   <div>Some text</div>
</div>
Run Code Online (Sandbox Code Playgroud)

assert chai

4
推荐指数
1
解决办法
5709
查看次数

只有在测试失败时,c ++ gtest才会在测试结束时输出其他信息

我想做这样的事情:

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)

c++ assert expectations googletest assertions

4
推荐指数
1
解决办法
1326
查看次数