我正在使用Google测试框架编写一些单元测试.但我想检查一些断言是否放置得很好并且有用.有没有办法在Google测试中捕获断言?
测试中的示例代码:
int factorial(int n){
assert(n >= 0);
//....
}
Run Code Online (Sandbox Code Playgroud)
然后测试:
#include <gtest/gtest.h>
TEST(FactorialTest,assertNegative){
EXPECT_ANY_THROW({
factorial(-1);
});
}
Run Code Online (Sandbox Code Playgroud)
但是EXPECT_ANY_THROW没有抓住断言但只有例外.我正在寻找一个捕获断言的解决方案.
在单元测试中,setup方法用于创建测试所需的对象.
在那些设置方法中,我喜欢使用断言:我知道我想在这些对象中看到什么值,我喜欢通过断言来记录这些知识.
在最近的文章中对单元测试调用其他单元测试在这里计算器,总的感觉似乎是,单元测试应该没有调用其他检查:这个问题的答案似乎是,你应该重构你的设置,使测试用例做不相互依赖.
但是"设置与断言"和单元测试调用其他单元测试没有太大区别.
因此我的问题是:在设置方法中使用断言是一种好习惯吗?
编辑:
答案结果证明:这不是一般的好习惯.如果需要测试设置结果,建议在断言中添加单独的测试方法(我勾选的答案); 为了记录意图,请考虑使用Java断言.
如何以编程方式为特定类启用断言,而不是指定命令行参数"-ea"?
public class TestAssert {
private static final int foo[] = new int[]{4,5,67};
public static void main(String []args) {
assert foo.length == 10;
}
}
Run Code Online (Sandbox Code Playgroud) 我想断言一个方法只被调用一次.我正在使用RhinoMocks 3.5.
这是我认为会起作用的:
[Test]
public void just_once()
{
var key = "id_of_something";
var source = MockRepository.GenerateStub<ISomeDataSource>();
source.Expect(x => x.GetSomethingThatTakesALotOfResources(key))
.Return(new Something())
.Repeat.Once();
var client = new Client(soure);
// the first call I expect the client to use the source
client.GetMeMyThing(key);
// the second call the result should be cached
// and source is not used
client.GetMeMyThing(key);
}
Run Code Online (Sandbox Code Playgroud)
我想如果第二次调用测试失败GetMeMyThing()的呼叫source.GetSomethingThatTakesALotOfResources().
我assert()在Linux中有一个问题:我可以在内核中使用它吗?
如果不是,你通常使用什么技术,例如我不想输入NULL指针?
是否有任何理由对多个断言进行分组:
public void shouldTellIfPrime(){
Assertions.assertAll(
() -> assertTrue(isPrime(2)),
() -> assertFalse(isPrime(4))
);
}
Run Code Online (Sandbox Code Playgroud)
而不是这样做:
public void shouldTellIfPrime(){
Assertions.assertTrue(isPrime(2));
Assertions.assertFalse(isPrime(4));
}
Run Code Online (Sandbox Code Playgroud) 我已经assert在JUnit测试套件中没有失败的Java 语句中进行了几次,因为在JUnit的JVM实例中没有启用断言.要清楚,这些是实现中的"黑盒子"断言(检查不变量等),而不是JUnit测试本身定义的断言.当然,我想在测试套件中捕获任何这样的断言失败.
显而易见的解决方案是要非常小心使用-enableassertions,每当我运行JUnit,但我更喜欢一个更强大的解决方案.一种替代方法是将以下测试添加到每个测试类:
@Test(expected=AssertionError.class)
public void testAssertionsEnabled() {
assert(false);
}
Run Code Online (Sandbox Code Playgroud)
有没有更自动的方法来实现这一目标?JUnit的系统范围配置选项?我可以在setUp()方法中添加动态调用吗?
代码可以使用其中的断言进行编译,并且可以在需要时激活/取消激活.
但是,如果我部署一个带有断言的应用程序并且那些被禁用,那么在那里被忽略的惩罚是什么?
上下文
在XUnit github中我发现了这个:添加Assert.Equal(预期,实际,消息)重载#350 (所以开发人员要求不存在的重载请参见下文)
引用答案:
我们相信自我记录的代码; 包括你的断言.
(所以XUnit团队拒绝它)
好,我知道了.我也相信自我记录代码.我还是找不到这个用例:
样品
// Arrange
// Create some external soap service client and its wrapper classes
// Act
// client.SomeMethod();
// Assert
// Sorry, soap service's interface, behaviour and design is *given*
// So I have to check if there is no Error, and
// conveniently if there is, then I would like to see it in the assertion message
Assert.Equal(0, client.ErrorMessage.Length); // Means no error
// I would like to have …Run Code Online (Sandbox Code Playgroud) 在尝试在Visual Studio Professonal 2008的测试功能中创建初始的失败单元测试Assert.ReferenceEquals()时,当对象实例不等于空引用时,我似乎无法正确地失败.请注意,object.ReferenceEquals()正确返回false此相同的比较.
这是我的班级代码:
public static class Project
{
public static object TheObject { get; set; }
public static void Startup(object theObject)
{
// ToDo: Project.Startup(): Test.
// ToDo: Project.Startup(): Implement.
}
}
Run Code Online (Sandbox Code Playgroud)
然后这是我的测试类的关键方面:
[TestClass()]
public class ProjectTest
{
[TestMethod()]
public void StartupTest()
{
object obj = "hello";
Project.Startup(obj);
Assert.ReferenceEquals(obj, Project.TheObject); // Test Passes!?!
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,该static void Startup(object)方法为空,因此static object TheObject永远不会设置该属性null.所以,显然,Assert.ReferenceEquals(obj, Project.TheObject)应该失败,但不知何故,这个测试通过了.
注意改变 …
assertions ×10
unit-testing ×5
java ×4
c# ×2
junit ×2
assert ×1
assertion ×1
c++ ×1
equals ×1
fixtures ×1
googletest ×1
junit5 ×1
linux ×1
linux-kernel ×1
mocking ×1
performance ×1
refactoring ×1
rhino-mocks ×1
tdd ×1
xunit ×1
xunit2 ×1