使用条件编译进行单元测试 [英] Using conditional compilation for unit testing

查看:88
本文介绍了使用条件编译进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道将Nunit代码放在必须相同的类中是个好主意吗
使用单元测试进行测试.那是同一类中的业务逻辑和测试代码.
可以使用部分类功能将测试代码放入另一个文件中.
可能是一些嵌套的类来放置实际的测试代码.并且可以有条件地编译此代码.我想这样做是因为我相信这种方法将使使用Nunit轻松测试私有方法成为可能.首先我想知道这可能吗?如果是的话,这种方法可能会给那些具有连续集成之类的大型项目带来特别的问题.
如果是愚蠢的问题,请告诉我.

I want to know is it good idea to put Nunit code in same class that has to be
tested using unit tests. That is business logic & test code in same class.
Partial class feature can be used to put the test code in to another file.
May be some nested class to put actual testing code. And this code can be compiled conditionally. I want to do this because I believe this approach will make it possible to easily test private methods with Nunit. First I want to know is it possible? If yes what can be the problelms with this approach specially for large projects having continueous integration and stuff like that.
If its stupid question please tell me.

推荐答案

是的,这是可能的.唯一的缺点是我看到了源代码的可读性和可维护性.这意味着如果将测试代码和实际代码结合起来,您的代码将变得凌乱.
希望对您有所帮助.
Yes that will be possible. the only drawback I see the the readability and maintainability of the source code. that means your code will be messy if you combine the test codes and the actual code.
I hope this will help.


通常,您不会将单元测试应用于私有方法.在那里进行单元测试以确保每个组件都根据其规范正确运行,即公共接口按预期运行.在开发私有方法时,通常应在不太正式的级别上对其进行测试,并将单元测试应用于私有方法有助于构建的公共输出.这就是为什么大多数测试框架(包括NUnit)没有技巧来测试非公共方法的原因之一.

对私有方法进行单元测试意味着即使您没有更改外部可见的任何内容,也无法在不重写测试的情况下更改类的内部工作方式.

您的问题的答案是它应该可以工作,但是提取测试和部署会很麻烦,并且可能会给您之后访问该项目的人们带来困惑.
Normally you would not apply unit testing to private methods. Unit testing is there to make sure that each component operates correctly according to its spec, i.e. the public interface works as it''s supposed to. Testing private methods should generally be done on a less formal level, when you''re developing them, with unit tests applied to the public output that the private methods help in constructing. That''s one reason why most testing frameworks (including NUnit) don''t have tricks to test non-public methods.

Unit testing your private methods means that you can''t change how the class works internally without rewriting the tests, even if you haven''t changed anything that is visible to the outside.

The answer to your question is that it should work, but it will be messy extracting the tests and the deployment and potentially confusing to people who visit the project after you.


这篇关于使用条件编译进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆