You can use a variable to hold the function you want to replace at test time. For example, like below. That said, I don’t think I’ve ever wanted to replace os.Open. What is the scenario you’re testing for? I’d suggest trusting that os.Open works as advertised. If you want to do something like have it return an in memory buffer instead of a file so that you can inspect it for writes, it’s better to rearchitect your function to take an io.Writer and have the caller open the file. You can then pass a bytes.Buffer or similar during testing.
For each call, I need to create a file and write strings into it.
but at time of running test case, i don’t want to create any run time resource like files.
Second thing, I could not create and read the file at the deployment environment.
otherwise, my test code is doing fine at local.