单元测试构造函数很重要吗? [英] Is it important to unit test a constructor?

查看:528
本文介绍了单元测试构造函数很重要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该单元测试构造函数吗?假设我有一个这样的构造函数:

  IMapinfoWrapper wrapper; 
public SystemInfo(IMapinfoWrapper mapinfoWrapper)
{
this.wrapper = mapinfoWrapper;
}

我需要为这个construtor写一个单元测试吗?我没有包装器变量的任何getter,所以我不需要测试。

解决方案

单元测试关于测试对象的公共状态,行为和交互。



如果你只是在你的构造函数中设置一个私有字段,有什么测试?

不要麻烦单元测试你的简单访问器和mutator。这只是愚蠢,它不帮助任何人。


Ought I to unit test constructors? Say I have a constructor like this:

        IMapinfoWrapper wrapper;
        public SystemInfo(IMapinfoWrapper mapinfoWrapper)
        {
            this.wrapper = mapinfoWrapper;
        }

Do I need to write a unit test for this construtor? I don't have any getters for the wrapper variable, so I don't need to test that.

解决方案

Unit testing is about testing the public states, behaviors, and interactions of your objects.

If you simply set a private field in your constructor, what is there to test?

Don't bother unit-testing your simple accessors and mutators. That's just silly, and it doesn't help anyone.

这篇关于单元测试构造函数很重要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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