assertions

How to catch an assert with Google test?

流过昼夜 提交于 2019-12-03 22:04:07
I'm programming some unit test with the Google test framework. But I want to check whether some asserts are well placed and are useful. Is there a way to catch an assert in Google test? Example code under test: int factorial(int n){ assert(n >= 0); //.... } And then the test: #include <gtest/gtest.h> TEST(FactorialTest,assertNegative){ EXPECT_ANY_THROW({ factorial(-1); }); } But EXPECT_ANY_THROW doesn't catch the assert but only exceptions. I'm searching for a solution to catch asserts. Steve Jessop Google test provides ASSERT_DEATH , EXPECT_DEATH and other related macros . This question and

JUnit: Enable assertions in class under test

女生的网名这么多〃 提交于 2019-12-03 09:30:55
I've been bit a few times by Java assert statements that didn't fail in the JUnit test suite because assertions weren't enabled in JUnit's JVM instance. To be clear, these are "black box" assertions inside implementations (checking invariants, etc) not the assertions defined by the JUnit tests themselves. Of course, I'd like to catch any such assertion failures in the test suite. The obvious solution is to be really careful to use -enableassertions whenever I run JUnit, but I'd prefer a more robust solution. One alternative is to add the following test to every test class: @Test(expected

Unit testing: Is it a good practice to have assertions in setup methods?

寵の児 提交于 2019-12-03 06:36:27
问题 In unit testing, the setup method is used to create the objects needed for testing. In those setup methods, I like using assertions: I know what values I want to see in those objects, and I like to document that knowledge via an assertion. In a recent post on unit tests calling other unit tests here on stackoverflow, the general feeling seems to be that unit tests should not call other tests: The answer to that question seems to be that you should refactor your setup, so that test cases do

PHPUnit: assertInstanceOf() not working

点点圈 提交于 2019-12-03 06:28:08
问题 I need to check if a variable is an object of the User type. User is my class $user my object $this->assertInstanceOf($user,User); This is not working, I have a use of undefined constant User - assumed 'User' Thanks in advance for your help 回答1: http://apigen.juzna.cz/doc/sebastianbergmann/phpunit/function-assertInstanceOf.html I think you are using this function wrong. Try: $this->assertInstanceOf('User', $user); 回答2: It's always a good idea to use ::class wherever you can. If you get used

How to implement XUnit descriptive Assert message?

雨燕双飞 提交于 2019-12-03 04:31:11
Context in XUnit github I found this: Add Assert.Equal(expected, actual, message) overload #350 (so a developer ask for a non existing overload see below) Quote from the answer: We are a believer in self-documenting code; that includes your assertions. (so the XUnit team rejects it) OK, I got it. I also believe the self documenting code. Still I can not find out this use case: Sample // 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

Asserting that a method is called exactly one time

☆樱花仙子☆ 提交于 2019-12-03 01:11:28
I want to assert that a method is called exactly one time. I'm using RhinoMocks 3.5. Here's what I thought would work: [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); } I want this test to fail if the second

Using assertion in the Linux kernel

雨燕双飞 提交于 2019-12-03 01:11:24
I have a question about assert() in Linux: can I use it in the kernel? If no, what techniques do you usually use if, for example I don't want to enter NULL pointer? The corresponding kernel macros are BUG_ON and WARN_ON . The former is for when you want to make the kernel panic and bring the system down (i.e., unrecoverable error). The latter is for when you want to log something to the kernel log (viewable via dmesg ). As @Michael says, in the kernel, you need to validate anything that comes from userspace and just handle it , whatever it is. BUG_ON and WARN_ON are to catch bugs in your own

PHPUnit: assertInstanceOf() not working

て烟熏妆下的殇ゞ 提交于 2019-12-02 21:01:25
I need to check if a variable is an object of the User type. User is my class $user my object $this->assertInstanceOf($user,User); This is not working, I have a use of undefined constant User - assumed 'User' Thanks in advance for your help http://apigen.juzna.cz/doc/sebastianbergmann/phpunit/function-assertInstanceOf.html I think you are using this function wrong. Try: $this->assertInstanceOf('User', $user); It's always a good idea to use ::class wherever you can. If you get used to this standard, you don't have to use FQCNs (fully qualified classnames), or escape backslashes. Also, IDEs

Unit testing: Is it a good practice to have assertions in setup methods?

拈花ヽ惹草 提交于 2019-12-02 20:13:56
In unit testing, the setup method is used to create the objects needed for testing. In those setup methods, I like using assertions: I know what values I want to see in those objects, and I like to document that knowledge via an assertion. In a recent post on unit tests calling other unit tests here on stackoverflow, the general feeling seems to be that unit tests should not call other tests: The answer to that question seems to be that you should refactor your setup, so that test cases do not depend on each other. But there isn't much difference in a "setup-with-asserts" and a unit test

Java/ JUnit - AssertTrue vs AssertFalse

半世苍凉 提交于 2019-12-02 15:24:09
I'm pretty new to Java and am following the Eclipse Total Beginner's Tutorials . They are all very helpful, but in Lesson 12, he uses assertTrue for one test case and assertFalse for another. Here's the code: // Check the book out to p1 (Thomas) // Check to see that the book was successfully checked out to p1 (Thomas) assertTrue("Book did not check out correctly", ml.checkOut(b1, p1)); // If checkOut fails, display message assertEquals("Thomas", b1.getPerson().getName()); assertFalse("Book was already checked out", ml.checkOut(b1,p2)); // If checkOut fails, display message assertEquals("Book