所以我看到Assert有几十种似乎基本上做同样事情的方法.
Assert.IsFalse( a == b );
Assert.IsTrue( a != b );
Assert.AreNotEqual( a, b );
Run Code Online (Sandbox Code Playgroud)
为什么?它只是更明确吗?应该何时使用各种方法?是否有正式的最佳实践文档?
所以,这是我的问题:我在python中编码,但我需要提高代码中某些部分的性能太慢.一个好的(简单的)解决方案似乎是使用cython; 我试了一下,结果很好.问题是我在我的python代码中使用assert语句.在使用cython之前,我可以使用-OO选项编译我的python代码,这样我就可以提供一个不执行任何断言测试的版本,并且仍然有调试的断言.但是在cython中编译的文件似乎总是执行断言.是否有一些选项可以传递给cython编译以删除(或不删除)断言?
TDPL描述了一种assert(false);陈述行为.这样的断言不会从发布版本中删除(与所有其他断言一样),并且实际上会立即停止程序.问题是为什么?为什么这么混乱的行为呢?他们可能会添加halt();或类似的东西,以便能够停止该程序.
有时,我在C++代码中使用以下构造:
if (something_goes_wrong)
{
assert(false);
return false;
}
Run Code Online (Sandbox Code Playgroud)
显然,这种结构在D中是不可能的.
UPDATE
要清楚.问题是为什么int x=0; assert(x);不会崩溃程序的发布版本,但assert(0);会?为何如此奇怪的语言设计决定?
我正在尝试在本教程之后向phpunit添加一个自定义断言,以验证作为字符串返回的复数(例如
"-123 + 456 1"
通过我正在测试的方法,对于实部和虚部都定义了精度.我已经将一个Complex.php类解析为实部和虚部,并将以下断言类组合为complexAssert.php:
require_once 'PHPUnit/Framework/Assert.php';
include_once getcwd().'/custom/Complex.php';
class complexAssert extends PHPUnit_Framework_Assert {
public function assertComplexEquals($expected, $actual, $message = '', $delta = 0)
{
$expectedComplex = new Complex($expected);
$actualComplex = new Complex($actual);
if (!($actualComplex->getReal() >= ($expectedComplex - $delta) &&
$actualComplex->getReal() <= ($expectedComplex + $delta))) {
return $this->fail($message);
}
if (!($actualComplex->getImaginary() >= ($expectedComplex - $delta) &&
$actualComplex->getImaginary() <= ($expectedComplex + $delta))) {
return $this->fail($message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的单元测试脚本:
require_once getcwd().'/custom/complexAssert.php';
require_once 'testDataFileIterator.php';
class EngineeringTest extends PHPUnit_Framework_TestCase
{ …Run Code Online (Sandbox Code Playgroud) node.js断言为单元测试提供的断言非常有限.甚至在我编写第一个测试之前,我已经创建了一些断言,因为很明显我会继续重复使用它们.
你能推荐一些好的断言库来测试常见的javascript情况(对象结构,对象类等等)吗?
理想情况下,它应该与nodeunit很好地集成(或者,更好地,扩展它的断言) - 我的断言没有,我必须将它们test作为额外的变量传递...
我见过的唯一一个是柴.你能说些什么呢?
在我的程序中,我从一个可运行的程序中创建了一个断言 - 其值为false - 但从未看到有关断言的任何控制台输出.我想知道我的断言是否为假,但似乎runnable正在捕获所有抛出的断言?
下面是我可以编写的最简单的示例程序来演示.(断言已启用.如果不是,则程序的行为会有所不同,并打印两行而不是一行).该程序的输出是.
即将断言False
而已.在那之后,断言语句抛出并被某些东西捕获,我从来不知道它.我想知道它,我做错了什么?
import java.nio.ByteBuffer;
import java.util.concurrent.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import javax.swing.*;
class App
{
private static final ScheduledExecutorService sExecutor =
Executors.newSingleThreadScheduledExecutor();
// Main
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { createAndShowGUI(); } });
}
// Swing GUI
private static void createAndShowGUI()
{
// Just create a swing thing. Boring
JFrame frame = new JFrame("Title String");
JLabel label = new JLabel("Hello World");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.getContentPane().setLayout(new FlowLayout()); …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习OCaml,我在使用断言语句时遇到了麻烦.在翻译中我可以使用它:
Zameers-MacBook-Air:~ zmanji$ ocaml
OCaml version 4.01.0
# let x = 1;;
val x : int = 1
# assert(x > 2);;
Exception: Assert_failure ("//toplevel//", 1, 0).
# ^D
Run Code Online (Sandbox Code Playgroud)
但是,当我将代码放在一个如下所示的文件中时:
let x = 1
assert(x > 2)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Zameers-MacBook-Air:Q4 zmanji$ ocaml test.ml
File "test.ml", line 2, characters 0-6:
Error: Syntax error
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
带有执行链的加特林场景。请求后,将保存返回的数据。稍后对其进行处理,并且根据处理结果,它应该失败或通过测试。
这似乎是最简单的情况,但是我找不到任何可靠的信息如何在exec块中使测试失败。assert打破了场景,看起来像是加特林(例如:异常抛出不仅使测试失败)。
例:
// The scenario consists of a single test with two exec creating the execChain
val scn = scenario("MyAwesomeScenario").exec(reportableTest(
// Send the request
exec(http("127.0.0.1/Request").get(requestUrl).check(status.is(200)).check(bodyString.saveAs("MyData")
// Process the data
.exec(session => {
assert(processData(session.attributes("MyData")) == true, "Invalid data");
})
))
Run Code Online (Sandbox Code Playgroud)
在方案上方的某个地方,“守护者失败,关闭了系统”。
现在,这似乎是一件有用且经常使用的事情-我可能会错过一些简单的事情。怎么做?
这是mongoDB驱动程序文档中的一个示例.我一直试图找出assert.equal在示例中的作用,但是Node.js网站上的官方文档对我没有太多帮助 - 官方文档说"用等同比较运算符测试浅,强制相等(==)." 所以我首先怀疑它会根据相等的真值重新判断是真还是假.但它似乎没有.
我确实看过这篇文章:在nodejs中使用断言模块?.它确实有所帮助 - 但还不够.我仍然不太明白"单元测试"是如何完成的.任何帮助将不胜感激,但坚实的例子将是非常有用的!
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
Grid = require('mongodb').Grid,
Code = require('mongodb').Code,
BSON = require('mongodb').pure().BSON,
assert = require('assert');
// Set up the connection to the local db
var mongoclient = new MongoClient(new Server("localhost", 27017), {native_parser: true});
// Open the connection to the server
mongoclient.open(function(err, mongoclient) {
// Get the first db and do an update …Run Code Online (Sandbox Code Playgroud) 您好,我是编程和尝试进行测试的新手,它检查项目列表中的任何项目是否存在于另一个列表中(在Python 2.7中使用unittest)。
例如,如果我有一个列表[[dog],“ cat”,“ frog],而我要测试的方法的结果是[” tiger“,” lion“,” kangaroo“,” frog],我希望测试失败,因为它包含上一个列表中的一项(“青蛙”)。我还希望测试告诉我两个列表都有哪些单词(即,哪些单词导致测试失败)。
我试过了:
self.assertIn(["cat", "dog"], method("cat dog tiger"))
Run Code Online (Sandbox Code Playgroud)
该方法的结果为[“ cat”,“ dog”,“ tiger”],但测试结果为失败,并显示:
AssertionError: ['cat', 'dog'] not found in ['cat', 'dog', 'tiger']
Run Code Online (Sandbox Code Playgroud)
我希望此测试返回“确定”,因为第二个列表中存在“猫”和“狗”。这似乎assertIn没有做什么,我还以为它会做(我认为这是检查是否有任何一个存在于b)。
反之亦然,当我希望它失败时会通过assertNotIn传递。
我已经搜索了一段时间,但是因为我不确定要查找的内容而很难找到。
谢谢您的阅读,我希望这是有道理的。
编辑:我已经放弃了克里斯的解决方案,它可以按我的意愿工作:
def myComp(list1, list2):
section = list(set(list1).intersection(list2))
Run Code Online (Sandbox Code Playgroud)
为了获取错误消息中重叠(即触发失败)的单词列表,我从此处添加了以下代码。如何在Python AssertionError中更改消息?:
try:
assert(len(section)==0)
except AssertionError as e:
e.args += ('The following are present in the processed text',
section)
raise
Run Code Online (Sandbox Code Playgroud)
结果正是我想要的:
AssertionError: ('The following are pressent in the processed text', ['dog',
'cat'])
Run Code Online (Sandbox Code Playgroud)