[UnitTest01]-UnitTest入门
来源:互联网 发布:禾田软件 编辑:程序博客网 时间:2024/06/07 05:22
在详细需求文档确定之后,测试人员需要根据文档写TestCase,在TDD开发模式下,开发人员需要根据文档写UnitTest。UnitTest是对需求的代码形式的描述,能否确保ProduceCode对需求的全面覆盖。对于开发人员来说,单元测试的重要性和ProduceCode同样重要,甚至更为重要。这个章节就由浅入深介绍下单元测试是怎么写的,在写的过程中会遇到哪些问题,如何解决和避免这些问题。
首先,我们写了如下一个非常简单的类,读者可以认为这是一个类库的其中的一个类。
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Justin.MathUtility.AppExample
{
public class Calcator
{
public static int Add(int x, int y)
{
return x + y;
}
}
}
这时候,我们需要写单元测试来验证Add方法。我们需要新建一个单元测试的工程。
添加单元测试工程的方式如上图所示,整个结构如下图所示。
单元测试的代码可以这样写。
我们可以通过运行测试得到如上图所示的测试结果。
这时候我们再加一个方法。
public class Calcator
{
public static int Add(int x, int y)
{
return x + y;
}
public static int Min(int x, int y)
{
return x - y;
}
}
测试代码如下:
public class CalcutorUnitTest
{
int a;
int b;
private void Init()
{
a = 1;
b = 2;
}
[TestMethod]
public void TestAdd()
{
Init();
int expected = 3;
int actual = Calcator.Add(a, b);
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void TestMin()
{
Init();
int expected = -1;
int actual = Calcator.Min(a, b);
Assert.AreEqual(expected, actual);
}
}
我们运行测试,依然可以通过。
这就是最简单的单元测试。
- [UnitTest01]-UnitTest入门
- unittest
- UnitTest
- unittest模块
- MapReduce UnitTest
- seleniumIDE + unittest
- python unittest
- unittest模块
- Python unittest
- unittest+HTMLTestRunner
- unittest理解
- Android UnitTest
- unittest单元测试
- python unittest
- 单元测试UnitTest
- Android-UnitTest
- UnitTest 笔记
- python unittest
- java中final关键字
- Java垃圾回收机制
- 题目1047:素数判定
- android 动画详解
- HashMap的底层实现
- [UnitTest01]-UnitTest入门
- 06-0. 混合类型数据格式化输入(5)
- 【ACdream】ACdream原创群赛(18)のAK's dream
- LINUX学习(2)
- leetcode题目思路以及部分解答(完)
- 静态成员变量
- 06-1. 简单计算器(20)
- 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- Leetcode: Valid Number