So a couple of weeks ago I create a thread here for feedback on my Roslyn code generator wrapper (can find it here https://www.reddit.com/r/csharp/comments/5ov5pc/created_a_wrapper_around_roslyn_and_would_love/). I got a lot of very nice feedback and also a couple of people that asked for examples on why and how you can use code generation.
So I started to work a bit on a “unit test generator” to show how you can use code generation in a fun way:
It’s far from finish and I think you can call it “SetUp generator” more than “unit test generator” right now. So here is how it work:
- You select your assembly
- The framework will look through your assembly for exported types (that is not static or abstract)
- For each type it find it will generate a unit test file
- In the unit test file it will generate a set up that that initialize the type by looking at the first constructor of the type.
- If the constructor requires any argument it will either mock them, initialize them or set default values.
And well that’s mostly it for now. I tried it on a project from work and it manage to generate at least 700 unit test files that matched the same directory/namespace structur as our regular project.
So if anyone would like to try it or have some funny ideas it would be fun to hear them. I worked on it most as an example at first but it’s actually pretty fun and I think you can do a lot more with it. We also only support MsTest or NUnit as test framework and Moq as mocking framework.