I am a newbie at TDD and Unit testing and currently reading James Greenings book TDD for Embedded C.
I really adopt to use dual targeting because it provides to test my case in both host and target device.
I am unit testing my amplifier driver and there are two requirements which are declared as
Driver shall read fault notification pin on amplifier IC manually, whenever requested.
Fault notification pin shall trigger the system when any fault occurred.
As you see, there should be a read_fault_notification function which is passed address and access the hardware. Everything is ok till here.
Here is my amplifier_get_fault_notification function in amplifier.c
amplifier.c
fault_state_t amplifier_get_fault_notification(amplifier_t device)
{
return device->fault.fault_state= amplifier_gpio_read(device->fault.fault_address);
}
Now, amplifier_gpio_read is linked to the target-specific function at hardware.c hence, Hardware abstraction provides.
static state_t target_device_gpio_read(uint8_t pin_name)
{
switch(pin_name)
{
case FAULT1:/*Fault pin @ Loudspeaker1*/
/*
* TO DO: Read IO for fault pin of Loudspeaker Amplifier1, if required.
*/
break;
case FAULT2:/*Fault pin @ Loudspeaker2*/
/*
* TO DO: Read IO for fault pin of Loudspeaker Amplifier2, if required.
*/
break;
case FAULT3:/*Fault pin @ Loudspeaker3*/
/*
* TO DO: Read IO for fault pin of Loudspeaker Amplifier3, if required.
*/
break;
case FAULT4:/*Fault pin @ Loudspeaker4*/
/*
* TO DO: Read IO for fault pin of Loudspeaker Amplifier4, if required.
*/
break;
default:
break;
}
return 1;/*hard coded*/
}
The question is how should I test this requirement "Driver shall read fault notification pin on amplifier IC manually, whenever requested". @kamilcuk tells me if you want to this event, do integration. If you want to test your function mock it but did not tell me how to I set up that test case.
Another question which seems more diffucult is how and where( in this driver layer or app layer ? ) should I test this requirement " Fault notification pin shall trigger the system when any fault occured."
Everybody tells me to use mock. The problem is not using a mock framework, this is simple one, the problem is although I've read lot of readings, still don't understand the concept of Mocking. Because, in James's book, he was filling the function body of hardware dependent codes to be act just like a real, this made confused my mind. Once writing hardware depending codes virtually, testing nested instructed functions according to this virtual functions did not make sense to me. What is the aim is here. Dual targeting approaches was good. I feel myself little bit idiot.