我何时应该使用Apache Commons的Validate.isTrue,何时应该使用'assert'关键字?
我想在我的单元测试框架中使用PHP的断言函数.它的优点是能够在错误消息中看到正在评估的表达式(包括注释).
问题是每个包含测试的方法可能有多个assert语句,我想跟踪已经运行了多少实际断言语句.assert没有给我一个方法来计算它运行了多少次,只计算它失败了多少次(在故障回调中).
我试图将assert语句抽象为一个函数,以便我可以添加一个计数机制.
private function assertTrue($expression) {
$this->testCount++;
assert($expression);
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为表达式中的任何变量现在都超出了范围.
$var = true;
$this->assertTrue('$var == true'); // fails
Run Code Online (Sandbox Code Playgroud)
关于如何在我的单元测试中使用断言同时能够计算实际测试数量的任何建议?
我提出的两个想法是让用户自己计算
$this->testCount++;
assert('$foo');
$this->testCount++;
assert('$bar');
Run Code Online (Sandbox Code Playgroud)
或者让用户在每个测试方法中只放置一个断言(然后我可以计算运行的方法的数量).但这些解决方案都不是非常可执行的,并且使编码更加困难.有关如何实现这一目标的任何想法?或者我应该从我的测试框架中剥离assert()?
NDEBUG定义宏时禁用标准C断言宏,意味着"不调试".这导致了非常糟糕的双重否定案例#ifndef NDEBUG //DebuggingCode #endif.似乎RELEASE本来是一个更好的术语选择,但我无法相信标准委员会会这样做而没有任何理由这样做....
我对Verilog完全不熟悉,所以请耐心等待.
我想知道Verilog中是否有断言声明.在我的测试平台中,我希望能够断言模块的输出等于某些值.
例如,
mymodule m(in, out);
assert(out == 1'b1);
Run Code Online (Sandbox Code Playgroud)
谷歌搜索给了我一些链接,但它们要么太复杂,要么似乎不是我想要的.
使用流畅的断言,我想断言给定的字符串包含两个字符串之一:
actual.Should().Contain("oneWay").Or().Should().Contain("anotherWay");
// eiter value should pass the assertion.
// for example: "you may do it oneWay." should pass, but
// "you may do it thisWay." should not pass
Run Code Online (Sandbox Code Playgroud)
仅当包含这两个值时,断言才会失败.由于没有Or()运算符,这不起作用(甚至不编译).
这是我现在这样做的方式:
bool isVariant1 = actual.Contains(@"oneWay");
bool isVariant2 = actual.Contains(@"anotherWay");
bool anyVariant = (isVariant1 || isVariant2);
anyVariant.Should().BeTrue("because blahblah. Actual content was: " + actual);
Run Code Online (Sandbox Code Playgroud)
这很冗长,必须手动创建"因为"参数以获得有意义的输出.
有没有办法以更易读的方式做到这一点?一个解决方案也应适用于其他流利的断言类型,如Be(),HaveCount()等...
我在.NET 3.5上使用FluentAssertions 2.2.0.0版,如果这很重要的话.
我有一个类库,它包含以下模型和方法
模型:
public class Employee {
public int EmpId { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
方法:
public class EmployeeService {
public List<Employee> GetEmployee() {
return new List<Employee>() {
new Employee() { EmpId = 1, Name = "John" },
new Employee() { EmpId = 2, Name = "Albert John" },
new Employee() { EmpId = 3, Name = "Emma" },
}.Where(m => m.Name.Contains("John")).ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个测试方法
[TestMethod()]
public void GetEmployeeTest() {
EmployeeService obj = new …Run Code Online (Sandbox Code Playgroud) Visual Studio /analyze为C/C++ 添加了代码分析(),以帮助识别错误的代码.这是一个非常好的功能,但是当您处理旧项目时,您可能会被警告的数量所淹没.
大多数问题都在产生,因为旧代码在方法或函数的开头做了一些ASSERT.
我认为这是代码中使用的ASSERT定义(来自afx.h)
#define ASSERT(f) DEBUG_ONLY((void) ((f) || !::AfxAssertFailedLine(THIS_FILE, __LINE__) || (AfxDebugBreak(), 0)))
Run Code Online (Sandbox Code Playgroud)
示例代码:
ASSERT(pBytes != NULL);
*pBytes = 0; // <- warning C6011: Dereferencing NULL pointer 'pBytes'
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种简单,清洁和安全的解决方案来解决这些警告,这并不意味着禁用这些警告.我是否提到当前代码库中出现了很多事件?
我一直遵循逻辑:如果断言失败,那么就有一个错误.根本原因可能是:
IE有没有其他可以得出的结论?是否存在断言失败并且没有错误的情况?
我有一个单元测试,用于从集合中获取对象的方法.这一直在失败,我不明白为什么,所以我在下面创建了一个非常简单的测试来创建2个供应商对象并测试他们是否相等,看看我是否能在我的代码测试中发现问题.但这次测试再次失败.谁能看到或解释原因?
[TestMethod()]
public void GetSupplierTest2()
{
Supplier expected = new Supplier();
expected.SupplierID = 32532;
expected.SupplierName = "Test 1"
Supplier actual = new Supplier();
actual.SupplierID = 32532;
actual.SupplierName = "Test 1"
Assert.AreEqual(expected, actual);
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我测试测试通过的对象的各个属性...
[TestMethod()]
public void GetSupplierTest2()
{
Supplier expected = new Supplier();
expected.SupplierID = 32532;
expected.SupplierName = "Test 1"
Supplier actual = new Supplier();
actual.SupplierID = 32532;
actual.SupplierName = "Test 1"
Assert.AreEqual(expected.SupplierID , actual.SupplierID );
Assert.AreEqual(expected.SupplierName , actual.SupplierName );
}
Run Code Online (Sandbox Code Playgroud) 假设我有一个python程序,其中assert用于定义事物应该是什么,并且我想用read-eval-loop捕获异常而不是AssertionError抛出.
当然,我可以
if (reality!=expectation):
print("assertion failed");
import pdb; pdb.set_trace();
Run Code Online (Sandbox Code Playgroud)
但是在代码中这比普通的更难看assert(reality==expectation).
我本可以在顶层pdb.set_trace()打电话except:,但后来我失去了失败的所有背景,对吧?(我的意思是,堆栈跟踪可以从异常对象中恢复,但不能从参数值中恢复,等等)
有没有像--magic命令行标志可以将python3解释器变成我需要的东西?
assert ×10
unit-testing ×4
c# ×2
.net ×1
c ×1
c++ ×1
collections ×1
combinations ×1
debugging ×1
java ×1
mstest ×1
pdb ×1
php ×1
python ×1
python-3.x ×1
validation ×1
verilog ×1
visual-c++ ×1
xunit ×1