views:

206

answers:

3

I am developing a library for serial communications with an FPGA via RS-422 port. This library is part of a bigger project.

I know I can test some parts of it, as the message generation. You set the inputs, and you can test if the array of bytes match the expected one. But, what if I wanted to make a more general test, how could I generate unit tests for it? do I have to write a simulator of the FPGA behaviour? is it possible to test the library in isolation?

+2  A: 

I would say that testing with an emulator or mock is going to let you exercise your code paths much more easily than prodding the real thing.

Ideally one uses something pre-existing. Otherwise it may not be a small amount of effort to build the emulation. However, if you don't understand the protocol well enough to emulate it then you surely can't communicate with it :-)

djna
As you pointed, in this case the problem is that perhaps the unit test would require a great effort...
yeyeyerman
Not sure sure it's **great** effort, starting with a mocking library of some sort. If the alternative is **no** testing, then we need to do the work. Possibly you can do the simple stuff against the real protocol, but the edge cases probably will need an emulator of some sort.
djna
Yeah, I agree with you, because I see is mocking or not unit testing. And being part of a complex system I prefer to isolate the possible errors as much as possible.
yeyeyerman
+1  A: 

You can make a mock class that would work as a simulator. Just make it so your write function processes the information you sent and saves the result on some kind of buffer, which could simply be a normal string. Then make a read function that reads the string, erases it and then returns it to you.

Leahn Novash
That is not unit testing anymore, but integration test - and therefore the right way. Unit test would be just the classes testing on their own.
BeowulfOF
@BeowulfOF That's what I has thinking... I don't want to end up doing unit testing of my unit tests :-)
yeyeyerman
Using mocks is not integration testing, for serious unit testing in general it's usually hard to avoid mocks in some guise.
djna
A: 

Do you want to unit test the driver for the FPGA, or do you want to unit test the protocol? Because if you want to test both, it would already look like a (system) integration test to me.

Yes, it is possible to test the communications library in isolation. Make sure your communications protocol is not too interwoven with the device driver; this way you can test it in isolation.

My suggestion:

  • unit test small parts; test them throroughly
  • check what you want of the integration test; lots of data or stress test? make it orthogonal to the unit test
  • if you mock up the hardware, make it very simple

The kind of errors which I would be interested in when testing the FPGA+communications library:

  • is it handling the protocol according to specs?
  • how does it behave on buffer overflow / underflow?
  • is the interrupt handling working as expected?
  • does it handle all rsx22 signals (break, parity, stop bits) correctly?

As for the simulation: I've had very good results running code within Matlab/Simulink (i.e. using TrueTime, a free plugin). Simulink can then be used to hook up to either hardware in the loop (HIL) or a model in the loop. Alternatively a software mockup could be used (but it is often hard to simulate asynchronous events which are sooo interesting if you're close-to-the-hardware; i.e. interrupt handlers)

Adriaan
I just want to test the protocol in the computer side. The FPGA is being developed by someone else and we both have the specs.
yeyeyerman