0

I am working on unit testing a code generator. A unit test's basic flow is:

  1. The Unit Test calls the appropriate method and code is generated. Easy enough.

  2. The Unit Test compiles the generated c# code (of step #1). If code compliles, proceed to step 3, else stop everything.

  3. If step#2 suceeded, the Unit Test then runs other, pre-written unit tests on the generated compiled code of step 2. For this I will utilize the solution described here: Running individual NUnit tests programmatically and NUnit API And Running Tests Programmatically .

The approach for step #2 is what this question is about: I am thinking I have two options (1) Run Visual Studio Command Line to compile the solution Or (2) use CSharpCodeProvider with CompilerParameters. Any recommendations would be greatly appreciated.

Community
  • 1
  • 1
chrisl08
  • 1,658
  • 1
  • 15
  • 24
  • How much do you care that compilation is tied to VS? Should I be able to run the tests without VS installed? – Mike Zboray Apr 19 '15 at 16:51
  • You could have a look at Roslyn. It's the new c# compiler which has an API you can use: https://github.com/dotnet/roslyn – Kenneth Apr 19 '15 at 16:51
  • Mike z, I do not really care if compilation of tied to VS. I see where you are going with this: If compilation needs to be tied to VS, then go with CSharpCodeProvider. But can CSharpCodeProvider/CompilerParameters offer everything that the VS command line offers? – chrisl08 Apr 19 '15 at 16:58
  • Kenneth, I had taken a look at Roslyn, it just seemed a little too much for what I want to do... – chrisl08 Apr 19 '15 at 17:00
  • What is the extent of the code that is being generated by each test method ? Are we talking about a single file or multiple interrelated files ? – Phil Gref Apr 19 '15 at 19:48
  • @PhilGref this is an ORM project that has been in use for quite some time now. The code generated is one c# class for each database table. The c# classes are generated and save to disk in a different assembly / c# project. The unit tests use a sample database of 5 tables. – chrisl08 Apr 20 '15 at 15:32

1 Answers1

1

I personally use Roslyn on a daily basis, and so I was tempted to go with Kenneth and recommend it, but, in your case, if the only information you want to know is if the code compiled, I would lean more towards the CSharpCodeProvider class, especially if each method that is being unit tested generates a single file of code. If you would have to to any kind of analysis on the generated code or if you , it might be worth using Roslyn, but I doubt this is your case. The only other pro Roslyn might bring you is that you can open up a whole project/solution directly instead of trying to compile every separate file, which might appeal to you (it's a lot simpler to use than you might think).

Besides this advice, all I can say is that if you just have to choose between CSharpCodeProvider and the Command Line option, I would definitely go with the CSharpCodeProvider, since it already wraps and exposes the data and operations relevant to you analysis (Are there any errors ? => let's check if compiledResults.Errors.Count == 0). You don't need to call an external process (the C# compiler) in order to get what you want, which makes it a much simpler option in my opinion, while also having a lot of flexibility (i.e. The CompilerParameters)

I don't know if you have started playing around with it, but you shouldn't have too many problems with this method. Hope this helps.

Phil Gref
  • 987
  • 8
  • 19
  • thank you. I am going with Roslyn due to the ablity to open up a whole project/solution and compile it. I did not know this. Thanks again – chrisl08 Apr 21 '15 at 04:27
  • Sure thing. Just keep the CSharpCodeProvider option open if Roslyn doesn't work out for some reason, I think that it's still the second best option. – Phil Gref Apr 21 '15 at 04:32