views:

305

answers:

7

Considering such code:

class ToBeTested {
public:
  void doForEach() {
    for (vector<Contained>::iterator it = m_contained.begin(); it != m_contained.end(); it++) {
       doOnce(*it);
       doTwice(*it);
       doTwice(*it);
    }
  }
  void doOnce(Contained & c) {
    // do something
  }
  void doTwice(Contained & c) {
    // do something
  }

  // other methods
private:
  vector<Contained> m_contained;
}

I want to test that if I fill vector with 3 values my functions will be called in proper order and quantity. For example my test can look something like this:

tobeTested.AddContained(one);
tobeTested.AddContained(two);
tobeTested.AddContained(three);

BEGIN_PROC_TEST()
SHOULD_BE_CALLED(doOnce, 1)
SHOULD_BE_CALLED(doTwice, 2)
SHOULD_BE_CALLED(doOnce, 1)
SHOULD_BE_CALLED(doTwice, 2)
SHOULD_BE_CALLED(doOnce, 1)
SHOULD_BE_CALLED(doTwice, 2)

tobeTested.doForEach()
END_PROC_TEST()

How do you recommend to test this? Are there any means to do this with CppUnit or GoogleTest frameworks? Maybe some other unit test framework allow to perform such tests?

I understand that probably this is impossible without calling any debug functions from these functions, but at least can it be done automatically in some test framework. I don't like to scan trace logs and check their correctness.

UPD: I'm trying to check not only the state of an objects, but also the execution order to avoid performance issues on the earliest possible stage (and in general I want to know that my code is executed exactly as I expected).

A: 

Some mocking frameworks allow you to set up ordered expectations, which lets you say exactly which function calls you expect in a certain order. For example, RhinoMocks for C# allows this.

I am not a C++ coder so I'm not aware of what's available for C++, but that's one type of tool that might allow what you're trying to do.

Doug R
+1  A: 

You could check out mockpp.

Tyler
+1  A: 

You should be able to use any good mocking framework to verify that calls to a collaborating object are done in a specific order.

However, you don't generally test that one method makes some calls to other methods on the same class... why would you?

Generally, when you're testing a class, you only care about testing its publicly visible state. If you test anything else, your tests will prevent you from refactoring later.

I could provide more help, but I don't think your example is consistent (Where is the implementation for the AddContained method?).

kbaribeau
"example is consistent" - AddContained() belongs to "// other methods" :)"publicly visible state" - Unnecessary calls may not influence state of an object, but they can slow down execution."...why would you?" - do you mean to test a caller of a function? That would be great too.
cos
A: 

http://msdn.microsoft.com/en-au/magazine/cc301356.aspx

This is a good article about Context Bound Objects. It contains some so advanced stuff, but if you are not lazy and really want to understand this kind of things it will be really helpful.

At the end you will be able to write something like: [CallTracingAttribute()] public class TraceMe : ContextBoundObject {...}

dimarzionist
thanks for the answer. I feel that .NET with reflection can help, but I use plain cpp (sometimes even without RTTI)
cos
+1  A: 

Instead of trying to figure out how many functions were called, and in what order, find a set of inputs that can only produce an expected output if you call things in the right order.

metao
order and quantity of calls may not influence internal state, but just lead to slow execution in future. this can be tested later of course with performance tests, but this can be too late and difficult
cos
A: 

You could use ACE (or similar) debug frameworks, and in your test, configure the debug object to stream to a file. Then you just need to check the file.

metao
I don't see any major differences from calling trace() within my functions to stdout or stderr and then checking these logs. I want to perform such test automatically after each build for example together with other unit tests.
cos
+1  A: 

If you're interested in performance, I recommend that you write a test that measures performance.

Check the current time, run the method you're concerned about, then check the time again. Assert that the total time taken is less than some value.

The problem with check that methods are called in a certain order is that your code is going to have to change, and you don't want to have to update your tests when that happens. You should focus on testing the actual requirement instead of testing the implementation detail that meets that requirement.

That said, if you really want to test that your methods are called in a certain order, you'll need to do the following:

  1. Move them to another class, call it Collaborator
  2. Add an instance of this other class to the ToBeTested class
  3. Use a mocking framework to set the instance variable on ToBeTested to be a mock of the Collborator class
  4. Call the method under test
  5. Use your mocking framework to assert that the methods were called on your mock in the correct order.

I'm not a native cpp speaker so I can't comment on which mocking framework you should use, but I see some other commenters have added their suggestions on this front.

kbaribeau
thanks for useful answers. now I know that my way is mocking frameworks. regarding time: first, i don't know target time to compare with. second: unexpected execution order at early stage, can lead to incorrect results later, with addition of another classes to the model, so why should i wait?
cos