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.
这个关于C ++单元测试框架的问题的答案表明我之前没有发生过这样的可能性:使用C ++ / CLI和NUnit为本机C ++代码创建单元测试。
We use NUnit for our C# tests, so the possibility of using it for C++ as well seems enticing.
我们在C#测试中使用NUnit,因此将它用于C ++的可能性似乎很诱人。
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?
我从未使用过托管C ++,所以我担心这种方法有任何实际限制吗?你们很多人都这样做吗?如果是这样,你的经历是什么样的?
5 个解决方案
#1
8
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.
我们一直这样做。我们有许多用C ++ / CLI编写的程序集,并使用C#和NUnit来测试它们。实际上,由于我们的目标是提供与C#配合良好的程序集,这样做可以确保我们已经完成了。
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.
您还可以在C ++ / CLI中编写NUnit测试并调用非托管C ++。可能最好的方法是将纯粹的非托管C ++保存在lib中,然后创建一个使用NUnit并链接到lib的测试程序集。
#2
1
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:
有两个缺点,在大多数情况下都不严重:
-
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.
如果你真的很挑剔,那么测试就不再是在纯粹的原生环境中运行了,所以有一种外部的可能性,即某些东西可能会在测试中运行但在运行时会失败。我认为你必须做一些相当异国情调的事情。
-
You rely on your C++ code being able to be included into a C++/CLI program. Sometimes this can have *es 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.
您依赖于您的C ++代码能够包含在C ++ / CLI程序中。有时这可能会与标题冲突,并强制您的代码使用UNICODE构建正常。一般来说,这是一件好事,因为它揭示了狡猾的代码(比如Win32调用的Ansi变体的不一致使用)。请记住,它只是包含的标题,所以它可能会显示的是你将标题暴露在太高的水平 - 你的一些包括应该在你的cpp实现文件中。
#3
1
My experience is that it is not possible to use NUnit to test C++ native code through C++/CLI because you will have trouble loading and using native code.
我的经验是,不可能使用NUnit通过C ++ / CLI测试C ++本机代码,因为您将无法加载和使用本机代码。
I have tried using nunit to load a basic c++/cli test dll linked against "just thread" which is an implementation of the c++ standard thread library. Test dlls won't even load with the latest version of NUnit (2.6.2).
我已经尝试使用nunit加载一个基本的c ++ / cli测试dll链接到“just thread”,它是c ++标准线程库的一个实现。测试dll甚至不会加载最新版本的NUnit(2.6.2)。
So definitely not the way to go!
所以绝对不是要走的路!
#4
0
I never used one, but isn't there a port? Perhaps http://cunit.sourceforge.net/documentation.html would work for you.
我从未使用过一个,但是没有端口?也许http://cunit.sourceforge.net/documentation.html对你有用。
#5
0
The biggest concern is the learning curve of the C++/CLI language (formerly Managed C++) itself, if the tests need to be understood or maintained by non-C++ developers.
如果非C ++开发人员需要理解或维护测试,那么最大的问题是C ++ / CLI语言(以前称为Managed C ++)本身的学习曲线。
It takes a minimum of 1-2 years of C++ OOP experience in order to be able to make contributions into a C++CLI/NUnit test project and to solve the various issues that arise between the managed-native code interfaces. (By contribution, I mean being able to work standalone and able to make mock objects, implement and consume native interfaces in C++/CLI, etc. to meet all testing needs.)
它需要至少1 - 2年的C ++ OOP经验才能为C ++ CLI / NUnit测试项目做出贡献,并解决托管本机代码接口之间出现的各种问题。 (通过贡献,我的意思是能够独立工作并能够制作模拟对象,在C ++ / CLI中实现和使用本机接口等,以满足所有测试需求。)
Some people may just never grasp C++/CLI good enough to be able to contribute.
有些人可能永远不会抓住C ++ / CLI足够好的贡献。
For certain types of native software libraries with very demanding test needs, C++/CLI/NUnit is the only combination that will meet all of the unit testing needs while keeping the test code agile and able to respond to changes. I recommend the book xUnit Test Patterns: Refactoring Test Code to go along this direction.
对于具有非常苛刻的测试需求的某些类型的本机软件库,C ++ / CLI / NUnit是唯一能够满足所有单元测试需求的组合,同时保持测试代码的灵活性并能够响应变化。我推荐使用xUnit测试模式:重构测试代码这本书来实现这个方向。
#1
8
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.
我们一直这样做。我们有许多用C ++ / CLI编写的程序集,并使用C#和NUnit来测试它们。实际上,由于我们的目标是提供与C#配合良好的程序集,这样做可以确保我们已经完成了。
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.
您还可以在C ++ / CLI中编写NUnit测试并调用非托管C ++。可能最好的方法是将纯粹的非托管C ++保存在lib中,然后创建一个使用NUnit并链接到lib的测试程序集。
#2
1
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:
有两个缺点,在大多数情况下都不严重:
-
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.
如果你真的很挑剔,那么测试就不再是在纯粹的原生环境中运行了,所以有一种外部的可能性,即某些东西可能会在测试中运行但在运行时会失败。我认为你必须做一些相当异国情调的事情。
-
You rely on your C++ code being able to be included into a C++/CLI program. Sometimes this can have *es 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.
您依赖于您的C ++代码能够包含在C ++ / CLI程序中。有时这可能会与标题冲突,并强制您的代码使用UNICODE构建正常。一般来说,这是一件好事,因为它揭示了狡猾的代码(比如Win32调用的Ansi变体的不一致使用)。请记住,它只是包含的标题,所以它可能会显示的是你将标题暴露在太高的水平 - 你的一些包括应该在你的cpp实现文件中。
#3
1
My experience is that it is not possible to use NUnit to test C++ native code through C++/CLI because you will have trouble loading and using native code.
我的经验是,不可能使用NUnit通过C ++ / CLI测试C ++本机代码,因为您将无法加载和使用本机代码。
I have tried using nunit to load a basic c++/cli test dll linked against "just thread" which is an implementation of the c++ standard thread library. Test dlls won't even load with the latest version of NUnit (2.6.2).
我已经尝试使用nunit加载一个基本的c ++ / cli测试dll链接到“just thread”,它是c ++标准线程库的一个实现。测试dll甚至不会加载最新版本的NUnit(2.6.2)。
So definitely not the way to go!
所以绝对不是要走的路!
#4
0
I never used one, but isn't there a port? Perhaps http://cunit.sourceforge.net/documentation.html would work for you.
我从未使用过一个,但是没有端口?也许http://cunit.sourceforge.net/documentation.html对你有用。
#5
0
The biggest concern is the learning curve of the C++/CLI language (formerly Managed C++) itself, if the tests need to be understood or maintained by non-C++ developers.
如果非C ++开发人员需要理解或维护测试,那么最大的问题是C ++ / CLI语言(以前称为Managed C ++)本身的学习曲线。
It takes a minimum of 1-2 years of C++ OOP experience in order to be able to make contributions into a C++CLI/NUnit test project and to solve the various issues that arise between the managed-native code interfaces. (By contribution, I mean being able to work standalone and able to make mock objects, implement and consume native interfaces in C++/CLI, etc. to meet all testing needs.)
它需要至少1 - 2年的C ++ OOP经验才能为C ++ CLI / NUnit测试项目做出贡献,并解决托管本机代码接口之间出现的各种问题。 (通过贡献,我的意思是能够独立工作并能够制作模拟对象,在C ++ / CLI中实现和使用本机接口等,以满足所有测试需求。)
Some people may just never grasp C++/CLI good enough to be able to contribute.
有些人可能永远不会抓住C ++ / CLI足够好的贡献。
For certain types of native software libraries with very demanding test needs, C++/CLI/NUnit is the only combination that will meet all of the unit testing needs while keeping the test code agile and able to respond to changes. I recommend the book xUnit Test Patterns: Refactoring Test Code to go along this direction.
对于具有非常苛刻的测试需求的某些类型的本机软件库,C ++ / CLI / NUnit是唯一能够满足所有单元测试需求的组合,同时保持测试代码的灵活性并能够响应变化。我推荐使用xUnit测试模式:重构测试代码这本书来实现这个方向。