Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limitations of using C++/CLI with NUnit

Tags:

nunit

c++-cli

This answer to a question about C++ unit test frameworks suggests a possibility that had not occurred to me before: using C++/CLI and NUnit to create unit tests for native C++ code.

We use NUnit for our C# tests, so the possibility of using it for C++ as well seems enticing.

I've never used managed C++, so my concern is are there any practical limitations to this approach? Are many of you doing this? If so, what was your experience like?

like image 910
Ferruccio Avatar asked Oct 28 '08 15:10

Ferruccio


2 Answers

We do this all of the time. We have many assemblies written with C++/CLI and use C# and NUnit to test them. Actually, since our goal is to provide assemblies that work well with C#, doing this makes sure that we have accomplished that.

You can also write NUnit tests in C++/CLI and call unmanaged C++. Probably the best way is the keep your pure unmanaged C++ in a lib, and then make a test assembly that uses NUnit and links to the lib.

like image 111
Lou Franco Avatar answered Sep 23 '22 06:09

Lou Franco


It works very well and gives you the benefit of having parameterised tests as well as a common test runner and framework if you're in a mixed environment.

There are two downsides, neither of which is serious for most cases:

  1. If you're being really picky, the tests are no longer being run in a purely native environment so there's an outside possibility that something may work under test but fail at runtime. I think you'd have to be doing something fairly exotic for this to matter.

  2. You rely on your C++ code being able to be included into a C++/CLI program. Sometimes this can have clashes with headers and it forces your code to build OK with UNICODE. In general, this is a good thing as it uncovers crufty bits of code (like inconsistent use of Ansi variants of Win32 calls). Bear in mind that it's only the headers being included so what it may well show is that you are exposing headers at too high a level - some of your includes should probably be within your cpp implementation files.

like image 36
Andy Dent Avatar answered Sep 23 '22 06:09

Andy Dent