Simulate Incoming File, Stub fopen() Function

Question:
Is there a way to simulate an incoming file, if I am looking to stub the fopen(...) function?
Answer:

To stub fopen, create a new environment or update your existing environment. In step 6 (choose UUTs & Stubs) of the environment wizard, add fopen to the list of library stubs. Make sure the checkboxes next to both are checked. Then build as usual. As long as one of your units under test makes a call to fopen, then it should appear as a stub.
 


 
There is no dedicated feature for simulating file opens/reads. 

If you wanted the file contents which are read to be completely configurable in the test case, a general approach would be to:

  • create a global variable in user code whose purpose will be to hold the file contents. This would presumably be some sort of string variable.
  • create a library stub for fopen().
  • add user code for fopen which will: read the contents of the global variable, write them to a temporary file, call the real fopen() to open the temporary file, and finally return the resulting FILE*.
  • in a test case, add user code to set the global to the desired file contents.
     


Article ID: 1501
Last updated: 2019-11-07
Revision: 4
VectorCAST -> C++ Unit Testing -> Simulate Incoming File, Stub fopen() Function
https://kb.vector.com/entry/1501/