我的工作,我要释放的开放源码库。我已经开始写测试为code,我想知道我是如何想的.NET对象来测试性能。可以说我有以下几点:
公共类Person {
#区域变量
私人字符串_name =的String.Empty;
私人字符串_surname =的String.Empty;
#区域属性
公共字符串名称{
得到{
返回_name;
}
}
公共字符串姓{
得到{
返回_surname;
}
组{
_surname =价值;
}
}
}
我有相关的code两个问题:
我如何单元测试,只是有一个吸气剂(如姓名中的例子)一个属性 我如何单元测试与二传手和吸气剂(像姓中的例子)一个属性我想测试是简单的,因为我已经发现了在其他code组Itellinsense做了错误的自动完成功能和属性未返回正确的变量。错误的属性
更新:
我说的不是简单的特性之一在这个例子中,他们也有他们背后的一些逻辑,是相当难以调试。编写使用setter方法来测试getter和反之亦然测试是不好的,因为如果有一个失败,我不知道该责怪哪个方法。我使用的属性,因为它们增加了作为公共变量,后来更多的逻辑已经无以复加。 解决方案我如何单元测试属性 只是有一个吸气剂(就像在名称 例如)
真是没有太大,如果你有一个setter测试不同。你只需要找到确定输出的另一种方式。可以在一个构造函数,或在对象上的其他设置器/操作的结果。
[测试]
公共无效NamePropTest()
{
者P =新的Person();
//有些code这里将设置Person对象
//让你知道什么名字会
Assert.AreEqual(一些已知的价值......,p.Name);
}
如果我们有制定者的姓名,但只有一个getter的全名,测试结果可能是这样的:
[测试]
公共无效NamePropTest()
{
者P =新的Person();
p.Name =肖恩;
p.Surname =宾;
Assert.AreEqual(西恩·潘,p.FullName);
}
I am working on a lib that I want to release in open source. I have started writing the tests for the code, and I was wondering how I am suppose to test a property in a .Net object. Lets say I have the following:
public class Person{
#region variables
private string _name = String.Empty;
private string _surname = String.Empty;
#region properties
public string Name{
get{
return _name;
}
}
public string Surname{
get{
return _surname;
}
set{
_surname = value;
}
}
}
I have two questions related to the code:
How do I Unit test a Property that just has a getter (Like Name in the example) How do I Unit test a Property with a setter and a getter (Like Surname in the example)I want to test properties that are that simple because I have already found errors in other code were Itellinsense did the wrong autocomplete and the property was not returning the correct variable.
Update:
I am not talking about simple properties as the one in the example, they do have some logic behind them and are quite hard to debug. Writing a test that uses the setter to test the getter and vice versa is not good because if there is a fail I won't know which method to blame. I am using properties because they were added as public variables and later more logic had to be added. 解决方案How do I Unit test a Property that just has a getter (Like Name in the example)
Really not so different from testing if you had a setter. you'll just need to find another way of determining the output. Could be in a ctor, or the result of other setters/operations on the object.
[Test]
public void NamePropTest()
{
Person p = new Person();
//Some code here that will set up the Person object
// so that you know what the name will be
Assert.AreEqual("some known value...", p.Name);
}
If we had setters for Name and SurName, but only a getter for FullName, the test could look like this:
[Test]
public void NamePropTest()
{
Person p = new Person();
p.Name = "Sean";
p.Surname = "Penn";
Assert.AreEqual("Sean Penn", p.FullName);
}