I'm using the Given/When/Then pattern to make test code much clearer. Since I'm writing those tests in C++ I chosed to use Google Test. With tests the pattern is clear, because I do sth like this:
TEST(TestFixture, TestName)
{
// Given
int a = 5;
int b = 6;
int expectedResult = 30;
// When
int result = Multiply(a, b);
// Then
EXPECT_EQ(expectedResult, result);
}
But with mocks it stops being clear since there appear some EXPECTs in the Given part. The Given part suppose to be a setup step. Please see an example:
TEST(TestFixture, TestName)
{
// Given
int a = 5;
int b = 6;
int expectedResult = 30;
MightCalculatorMock mock;
EXPECT_CALL(mock, multiply(a,b))
.WillOnce(Return(expectedResult));
// When
int result = Multiply(mock, a, b);
// Then
EXPECT_EQ(expectedResult, result);
}
Is this approach correct? How the Given/When/Then comments should be placed in the test code, where?
The EXPECT_CALL macro can be thought of as a way of testing interaction between a class and another class. As such, if you are using it with another EXPECT macro, then your test is likely testing two things, which is why it appears to conflict with the "Given-When-Then" paradigm (also known as "Arrange-Act-Assert").
If you just need to set up some behavior on your mock object for testing, use the ON_CALL macro instead:
TEST(TestFixture, TestName)
{
// Given
int a = 5;
int b = 6;
int expectedResult = 30;
MightCalculatorMock mock;
ON_CALL(mock, multiply(a,b))
.WillByDefault(Return(expectedResult));
// When
int result = Multiply(mock, a, b);
// Then
EXPECT_EQ(expectedResult, result);
}
If you are actually looking to test the iteraction between your system under test and some other collaborator, you can use an "Arrange-Expect-Act" pattern:
TEST(TestFixture, CalculatorIsCalledProperly)
{
// Arrange
int a = 5;
int b = 6;
int expectedResult = 30;
MightCalculatorMock mock;
// Expect
EXPECT_CALL(mock, multiply(Eq(a),Eq(b)));
// Act
int result = Multiply(mock, a, b);
}
Related
I have a function that I'm testing, f1().
f1() calls g1() which can return a few different values.
How do I mock g1() so I can iterate through the different values it returns in order to test the paths inside f1()?
int f1()
{
int res = g1();
int ret = 0;
switch(res):
{
case 0:
// ret = something
case 1:
// ret = something else
default:
// ret = bad result
}
return ret;
}
Probably you can try cmocka tool to do the above mentioned use case.
I want to run unit tests via MS Test (from windows console) in a way that I can stop/kill the test execution whenever the failed tests count exceeds certain threshold value.
For my use case there is no point to keep running tests when certain percentage of the tests already failed.
I can only think in creating a new console app to wrap the mstest.exe execution, so I can parse the standard output in real-time,
and eventually kill the process, for example:
var pi = new ProcessStartInfo()
{
FileName = MS_TEST,
UseShellExecute = false,
RedirectStandardOutput = true,
Arguments = MS_TEST_ARGS
};
int passed = 0; int failed = 0;
using (var process = Process.Start(pi))
{
while (!process.StandardOutput.EndOfStream)
{
string line = process.StandardOutput.ReadLine();
if (line.Contains("Passed"))
passed++;
if (line.Contains("Failed"))
failed++;
if (failed >= THRESHOLD)
{
process.Kill();
break;
}
}
}
Can anyone suggest a better way for doing this? I don't think this is natively supported by MsTest.
PowerShell seems to be an option, but the stdout redirect is not trivial.
Update
As a note, I cannot modify the test code, I need this to be done without modifying the tests code in any way.
Create a BaseTestClass which contains a method responsible for killing the process that runs the tests.
using System.Diagnostics;
namespace UnitTestProject1
{
public class BaseTestClass
{
private readonly int _threshold = 1;
private static int _failedTests;
protected void IncrementFailedTests()
{
if (++_failedTests >= _threshold)
Process.GetCurrentProcess().Kill();
}
}
}
Your must inherit all your test classes from BaseTestClass and use the [TestCleanup] attribute. The TestCleanup() method is evaluated when a test defined in the DemoTests class has finished running. Is in that method where we evaluate the output of the test that has just finished. If it failed, we kill the process responsible for running the tests.
In the following example we have defined three tests. The second test, Test_Substracting_Operation(), is intended to fail intentionally, so the third test will never be run.
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject1
{
[TestClass]
public class DemoTests : BaseTestClass
{
public TestContext TestContext { get; set; }
[TestCleanup]
public void TestCleanup()
{
if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed)
{
IncrementFailedTests();
}
}
[TestMethod]
public void Test_Adding_Operation()
{
// Arrange
int x = 1;
int y = 2;
// Act
int result = x + y;
// Assert
Assert.AreEqual(3, result);
}
[TestMethod]
public void Test_Substracting_Operation()
{
// Arrange
int x = 1;
int y = 2;
// Act
int result = x - y;
// Assert
Assert.AreEqual(100, result);
}
[TestMethod]
public void Test_Multiplication_Operation()
{
// Arrange
int x = 1;
int y = 2;
// Act
int result = x * y;
// Assert
Assert.AreEqual(2, result);
}
}
}
Is it possible to get the class/struct/other variables value during runtime in dlang to get/set its value? If yes how to do that please provide example.
And also is it possible to get the runtime variable value?
Ex:
class S{ int svariable = 5;}
class B { int bvariable = 10;}
void printValue(T, T instanceVariable, string variableName) {
writeln("Value of ", variableName, "=", instanceVariable.variableName);
}
Output:
Value of svariable = 5;
Value of bvariable = 10;
There is a library named witchcraft that allows for runtime reflection. There are examples of how to use it on that page.
I'd first recommend trying a reflection library like #mitch_ mentioned. However, if you want to do without an external library, you can use getMember to get and set fields as well as invoke functions:
struct S {
int i;
int fun(int val) { return val * 2; }
}
unittest {
S s;
__traits(getMember, s, "i") = 5; // set a field
assert(__traits(getMember, s, "i") == 5); // get a field
assert(__traits(getMember, s, "fun")(12) == 24); // call a method
}
I would like to create BDD for the following Boolean function:
F = (A'B'C'D') OR (A'B C) OR (C' D') OR (A)
I managed to create only F = (A'B'C'D') with the following code but how to add other product terms to the existing BDD?
int main (int argc, char *argv[])
{
char filename[30];
DdManager *gbm; /* Global BDD manager. */
gbm = Cudd_Init(0,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0); /* Initialize a new BDD manager. */
DdNode *bdd, *var, *tmp_neg, *tmp;
int i;
bdd = Cudd_ReadOne(gbm); /*Returns the logic one constant of the manager*/
Cudd_Ref(bdd); /*Increases the reference count of a node*/
for (i = 3; i >= 0; i--) {
var = Cudd_bddIthVar(gbm,i); /*Create a new BDD variable*/
tmp_neg = Cudd_Not(var); /*Perform NOT boolean operation*/
tmp = Cudd_bddAnd(gbm, tmp_neg, bdd); /*Perform AND boolean operation*/
Cudd_Ref(tmp);
Cudd_RecursiveDeref(gbm,bdd);
bdd = tmp;
}
bdd = Cudd_BddToAdd(gbm, bdd);
print_dd (gbm, bdd, 2,4);
sprintf(filename, "./bdd/graph.dot");
write_dd(gbm, bdd, filename);
Cudd_Quit(gbm);
return 0;
}
Build every conjunction independently so that you get conj0 to conj3 make sure to only negate the correct literals. I'm not particularly versed in C and don't have a development environment setup right now so you will need to make some corrections.
I will use the following mapping
A <=> BDD(0)
B <=> BDD(1)
C <=> BDD(2)
D <=> BDD(3)
Build conj0 the way you do it now in your for loop. Make sure conj0 = bdd afterwards.
For conj1 which will encode (A' B C) use
bdd = Cudd_IthVar(gbm, 0);
bdd = Cudd_Not(bdd);
tmp = Cudd_And(gbm, bdd, Cudd_IthVar(gbm, 1));
Cudd_Ref(tmp);
Cudd_Deref(gbm, bdd);
bdd = tmp;
tmp = Cudd_And(gbm, bdd, Cudd_IthVar(gbm, 2));
Cudd_Ref(tmp);
Cudd_Deref(gbm, bdd);
bdd = tmp;
conj1 = bdd;
Do the same for conj2 and conj3.
After you've got all the conjunctions build build the top level disjunction by using Cudd_bddOr().
Also make sure that you get the Cudd_Ref() and Cudd_Deref() right otherwise you'll leak memory.
If you are only interested in that particular function, here's a way to build it and inspect it:
#include <stdio.h>
#include <stdlib.h>
#include "cudd.h"
int main(void) {
/* Get set. */
DdManager * mgr = Cudd_Init(4,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0);
DdNode *a = Cudd_bddIthVar(mgr, 0);
DdNode *c = Cudd_bddIthVar(mgr, 1);
DdNode *b = Cudd_bddIthVar(mgr, 2);
DdNode *d = Cudd_bddIthVar(mgr, 3);
char const * const inames[] = {"a", "c", "b", "d"};
/* Build BDD. */
DdNode * tmp = Cudd_bddIte(mgr, c, b, Cudd_Not(d));
Cudd_Ref(tmp);
DdNode * f = Cudd_bddOr(mgr, a, tmp);
Cudd_Ref(f);
Cudd_RecursiveDeref(mgr, tmp);
/* Inspect it. */
printf("f");
Cudd_PrintSummary(mgr, f, 4, 0);
Cudd_bddPrintCover(mgr, f, f);
char * fform = Cudd_FactoredFormString(mgr, f, inames);
printf("%s\n", fform);
/* Break up camp and go home. */
free(fform);
Cudd_RecursiveDeref(mgr, f);
int err = Cudd_CheckZeroRef(mgr);
Cudd_Quit(mgr);
return err;
}
Note the choice of (optimal) variable order. You should see this output:
f: 5 nodes 1 leaves 12 minterms
1--- 1
-11- 1
-0-0 1
a | (c & b | !c & !d)
I've started to read "The art of unit testing" and I'm trying to implement this piece of code:
[Test]
public void ReturnResultsFromMock()
{
MockRepository mocks = new MockRepository();
IGetResults resultGetter = mocks.DynamicMock<IGetResults>();
using(mocks.Record())
{
resultGetter.GetSomeNumber("a");//#1
LastCall.Return(1);
resultGetter.GetSomeNumber("a");//#2 how it should work?
LastCall.Return(2);
resultGetter.GetSomeNumber("b");
LastCall.Return(3);
}
int result = resultGetter.GetSomeNumber("b");
Assert.AreEqual(3, result);
int result2 = resultGetter.GetSomeNumber("a");
Assert.AreEqual(1, result2);
int result3 = resultGetter.GetSomeNumber("a");
Assert.AreEqual(2, result3);
}
I get this error message after running my test
AOUT.Loga.Tests.LogAnalyzerTest.ReturnResultsFromMock:
Expected: 2
But was: 1
Looks like you try to implement ordered sequence of calls (see details here):
First you call GetSomeNumber("a") should returns 1
Then call GetSomeNumber("a") again, result will be 2
And only then GetSomeNumber("b"), result will be 3
Is so, try to replace using(mocks.Record()) with using(mocks.Ordered()). But, this will work only in this sequence of calls
At your example, you are using using(mocks.Record()), so every call to GetSomeNumber("a") will return 2 acording to your configuration. You're override first GetSomeNumber("a") by second one. Here are correct assumptions:
int result = resultGetter.GetSomeNumber("b");
Assert.AreEqual(3, result);
int result2 = resultGetter.GetSomeNumber("a");
Assert.AreEqual(2, result2); // every call to GetSomeNumber("a") will returns 2
int result3 = resultGetter.GetSomeNumber("a");
Assert.AreEqual(2, result3);
You may try using the simpler Rhino Mocks AAA syntax, your code will look something like this (take a look also at this question):
// Arrange
var resultGetter = MockRepository.GenerateMock<IGetResults>;
resultGetter.Expect(x => x.GetSomeNumber("b")).Return(3);
resultGetter.Expect(x => x.GetSomeNumber("a")).Return(1).Repeat.Once();
resultGetter.Expect(x => x.GetSomeNumber("b")).Return(2).Repeat.Once();
// Act
int result = resultGetter.GetSomeNumber("b");
// Assert
Assert.AreEqual(3, result);
// Act
int result2 = resultGetter.GetSomeNumber("a");
// Assert
Assert.AreEqual(1, result2);
// Act
int result3 = resultGetter.GetSomeNumber("a");
// Assert
Assert.AreEqual(2, result3);