You can add search paths that the GCC preprocessor use to look for header files with the -I option, and then you could use angle-brackets when including the file, then the preprocessor should look for files in the same directory as the source file last.
In other words add e.g. -Itest to add the test directory to the include file search path. Then use #include <foo.h> instead. And when you want to use product/foo.h instead, just remove the -Itest option and add -Iproduct instead.
There is also another way, that involved conditional compilation and the solution suggested by Lanting.
You could have e.g.
#ifdef TEST
# include "../test/foo.h"
#else
# include "foo.h"
#endif
Then to build with test/foo.h you defined the macro TEST, which can be done on the command line using the -D option:
$ gcc -DTEST ...
To use the product/foo.h just remove the option so TEST is no longer defined.