-2

I'm hoping that I can use stylecop to prevent disk IO in unit tests, by preventing explicit method calls that I define.

Have very little experience with stylecop but from what I've gathered it should be able to do this kind of task (prevent given method calls). Searched stackoverflow and google without success.

Is this possible and if so, how?

Ivar
  • 119
  • 1
  • 1
  • 6

2 Answers2

1

Stylecop is a tool that monitors code for a consistent style and raises build warnings / errors.

I don't believe that it can be used to prevent disk IO in unit tests.

From the old project home on codeplex:

StyleCop analyzes C# source code to enforce a set of style and consistency rules. It can be run from inside of Visual Studio or integrated into an MSBuild project. StyleCop has also been integrated into many third-party development tools.

NikolaiDante
  • 18,469
  • 14
  • 77
  • 117
  • Sounds like it should be able to detect it. It monitors for specific method calls and raises error if they are there. So for example, if I can get stylecop to prevent string.Format I should be able to prevent some other method calls as well, right? – Ivar Jan 12 '16 at 10:51
  • It's about code formatting, not code execution. How it looks, not what it does. @jessehouwing 's answer has merit. – NikolaiDante Jan 12 '16 at 10:53
1

Stylecop, not likely, but you could probably implement this partially using a Roslyn rule (if you're targeting Visual Studio 2015) or a FxCop rule (if you're targeting Visual Studio 2013 or older).

What will be hard to validate are situations where a test calls a method, which calls a method, which raises an event, which causes IO. Simpler situations where a test calls a method which accesses System.IO is pretty easy.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • "Simpler situations where a test calls a method which accesses System.IO is pretty easy." Do you know how I set this up in stylecop? Because I think our legacy tests are mostly accessing files on disk the same way, so raising an error for that particular way would get us a long way. – Ivar Jan 12 '16 at 10:46
  • 1
    Not in StyleCop. I was talking fxcop or Roslyn. – jessehouwing Jan 12 '16 at 11:38