[erlang-questions] Fault injection for testing?

James Churchman <>
Fri Dec 24 21:11:34 CET 2010


File Write looks easy enough with meck, the trick is just to passthrough all the other functions & unstick:

1) Unstick File module, so it can be modified : code:unstick_mod(file).
2) Mock File module, but with passthrough so all file handling functions still work :  meck:new(file,[passthrough]).
3) Replace the write_file function : meck:expect(file, write_file, fun(_,_) -> {error, my_error_code} end).
Then  meck:unload(file). After your done.

read_file seems to work ok as well

What would be really cool is if you could use typer to ask for the spec of specific functions and then generate the possible outcomes.

Unfortunately typEr, although entirely written in erlang, appears to be a comandline only app! It would seem sensible to be able to do typer_utils:function_spec(lists,min) etc.. and it return the spec

James


On 21 Dec 2010, at 00:00, Michal Ptaszek wrote:

> Hi!
> 
> You might want to use e.g. meck
> https://github.com/esl/meck
> 
> to simulate some uncommon scenarios. 
> Nonetheless, as mocking 'file' module might be
> a little bit tricky, I would recommend creating an
> interface module, which should be transparent
> during the normal life cycle and easily mocked
> when performing the tests, e.g.:
> 
> -module(file2).
> -compile(export_all).
> write_file(Filename, Data) ->
>   file:write_file(Filename, Data).
> 
> Then, you can easily run:
> meck:new(file2).
> meck:expect(file2, write_file, {error, my_error_code}).
> 
> Best regards,
> Michal Ptaszek
> 
> 
> On Dec 20, 2010, at 5:32 AM, Attila Rajmund Nohl wrote:
> 
>> Hello!
>> 
>> Testing error handling is complicated, because it might be hard to
>> cause the error situation in the first place, which in my case it's a
>> file writing error:
>> 
>>   case file:write_file(FileName, <<Index:8/integer>>) of
>> 	ok -> ok;
>> 	Error ->
>> 	    io:format(standard_error, "~p ~p Error: '~p' ~n", [?MODULE, ?LINE, Error]),
>> 	    exit(open_lastwritten_file)
>>   end.
>> 
>> I could modify my code to fail is some debug flag is turned on, but it
>> has a (minor) performance impact. My other idea is to make sure that
>> the write_file fails by injecting the fault into the OTP library, i.e.
>> before the testcase runs in the automatic tests, load a faulty 'file'
>> module where the write_file function fails (I happen to know the
>> filename, so it won't fail for other uses), execute the testcase, then
>> remove the faulty file module.
>> 
>> Do you have any other ideas on how to test these kind of error
>> handling code? Testing the function separately, outside the whole
>> application doesn't do it, because it wouldn't detect possible
>> deadlocks (the original version of this code actually had a deadlock).
>> 
>> ________________________________________________________________
>> erlang-questions (at) erlang.org mailing list.
>> See http://www.erlang.org/faq.html
>> To unsubscribe; mailto:
>> 
> 



More information about the erlang-questions mailing list