结对编程2--单元测试

a、需求分析:

1.把计算模块提取出来，单独创建一个类。
2.针对提取出来的计算类的接口函数做单元测试。
3.通过单元测试代码，测试加法是否能正确工作；
4.通过单元测试代码，测试加减乘除功能。
5.通过单元测试代码，测试计算类对于各种参数的支持。

b、设计测试框架, 模拟测试数据：

由于前几次的代码是用C写的，这次测试将计算模块抽出写成了C++。


class TrivialCalculator
{
public:
double Subtract(double x, double y);
double Multiply(double x, double y);
double Divide(double x, double y);
int gcd(int x, int y);
string cal(char op,int z,int y);
string zfcal(char op, int z1, int y1, int z2, int y2);
string calinput(string s);
};


c、计算模块的测试用例及运行结果:

TEST_METHOD(gcdTest)
{
//TrivialCalculator test;
int actual = test->gcd(6,3);
int expected = 3;
Assert::AreEqual(expected, actual);
}


TEST_METHOD(calTest)
{
string actual = test->cal('+', 3, 4);
string expected = "7";
Assert::AreEqual(expected, actual);
actual = test->cal('+', 3, 4);
expected = "8";
Assert::AreNotEqual(expected, actual);
actual = test->cal('-', 9, 8);
expected = "1";
Assert::AreEqual(expected, actual);
actual = test->cal('-', 9, 8);
expected = "2";
Assert::AreNotEqual(expected, actual);
actual = test->cal('*', 9, 8);
expected = "72";
Assert::AreEqual(expected, actual);
actual = test->cal('-', 9, 8);
expected = "2";
Assert::AreNotEqual(expected, actual);
actual = test->cal('/', 9, 7);
expected = "9/7";
Assert::AreEqual(expected, actual);
actual = test->cal('/', 9, 8);
expected = "2";
Assert::AreNotEqual(expected, actual);
}


TEST_METHOD(zfcalTest)
{
//TrivialCalculator test;
string actual = test->zfcal('+', 1, 3,1,3);
string expected = "6/9";
Assert::AreEqual(expected, actual);
actual = test->zfcal('+', 1, 3, 1, 3);
expected = "1/3";
Assert::AreNotEqual(expected, actual);
actual = test->zfcal('-', 2, 3, 1, 3);
expected = "3/9";
Assert::AreEqual(expected, actual);
actual = test->zfcal('-', 2, 3, 1, 3);
expected = "2";
Assert::AreNotEqual(expected, actual);
actual = test->zfcal('*', 2, 3, 1, 3);
expected = "2/9";
Assert::AreEqual(expected, actual);
actual = test->zfcal('*', 2, 3, 1, 3);
expected = "2";
Assert::AreNotEqual(expected, actual);
actual = test->zfcal('/', 2, 3, 1, 3);
expected = "6/3";
Assert::AreEqual(expected, actual);
actual = test->zfcal('*', 2, 3, 1, 3);
expected = "2";
Assert::AreNotEqual(expected, actual);
}


TEST_METHOD(calinputTest)
{
//TrivialCalculator test;
string actual = test->calinput("1++2");
string expected = "输入错误！";
Assert::AreEqual(expected, actual);
actual = test->calinput("1++2");
expected = "输入正确！";
Assert::AreNotEqual(expected, actual);
actual = test->calinput("1/2+8/0");
expected = "输入错误！";
Assert::AreEqual(expected, actual);
}


e、小结与感受

（1）以前学习java的时候记得老师有讲过测试这一块，但是懂得的也不多，代码覆盖率一开始不懂，我们都上网查了很多资料，但是按照网页教程操作，问题还是没能解决。基础不够扎实，还是需要反复多加练习，贵在持之以恒。

（2）对伙伴的评价：

(1) 良好的设计

(2) 编码规范

(3) 必要的注释

PSP表格

posted on 2017-03-29 13:01  曾丽君  阅读(256)  评论(1编辑  收藏  举报