a******e 发帖数: 124 | 1 以前写程序test时候只是在main函数里简单输出一下看看对不对,最近想学一下unit
test,这个有什么格式要求吗(我看有些人用assert())?比如用c++语言想写一个
binary search的unit test该如何写,谢谢!
试着写了一个,不知道这样算不算unit test
#include
using namespace std;
int BinarySearch(int arr[], int size, int target){
int high=size-1;
int low=0;
int mid;
while(low<=high){
mid=low+(high-low)/2;
if(arr[mid]==target){
return mid;
}
else if(arr[mid]>target){
high=mid-1;
}
else{
low=mid+1;
}
}
return -1;
}
bool BinarySearch_Test (int arr[], int n, int target, int index){
bool result=true;
int output=BinarySearch(arr, n, target);
cout<<"Array is: "<
for(int i=0; i
cout<
if((i+1)%20==0) cout<
}
cout<
cout<<"Target: "<
cout<<"Expected index: "<
cout<<"Output index: "<
if(index==output)
cout<<" Pass!"<
else{
cout<<" Fail!"<
result=false;
}
cout<
return result;
}
void Run_Test(){
int n, target, index;
//test case 1: normal case
int arr1[]={1, 3, 4, 5, 7, 10, 23, 25};
n=8; target=5; index=3;
bool test1=BinarySearch_Test(arr1, n, target, index);
//test case 2: test the first element
int arr2[]={1, 3, 4, 5, 7, 10, 23, 25};
n=8; target=1; index=0;
bool test2=BinarySearch_Test(arr2, n, target, index);
//test case 3: test the last element
int arr3[]={1, 3, 4, 5, 7, 10, 23, 25};
n=8; target=25; index=7;
bool test3=BinarySearch_Test(arr3, n, target, index);
//test case 4: test for large array
int arr4[100];
for(int i=1; i<100; i++){
arr4[i]=arr4[i-1]+1;
}
n=100; target=55; index=55;
bool test4=BinarySearch_Test(arr4, n, target, index);
//test case 5: test for array with repeat elements
int arr5[]={3, 3, 4, 5, 5, 10, 10, 25};
n=8; target=4; index=2;
bool test5=BinarySearch_Test(arr5, n, target, index);
if(test1&&test2&&test3&&test4&&test5) cout<<"All Test Pass!"<
else cout<<"Test Fail!"<
}
int main(int argc, char **argv){
Run_Test();
} | C***y 发帖数: 2546 | 2 找一个unit test framework。基本上就是写各种test case作为参数调用要测的函数,
然后拿输出结果和预期的正确结果作比较,不一致的话,test就通过不了
【在 a******e 的大作中提到】 : 以前写程序test时候只是在main函数里简单输出一下看看对不对,最近想学一下unit : test,这个有什么格式要求吗(我看有些人用assert())?比如用c++语言想写一个 : binary search的unit test该如何写,谢谢! : 试着写了一个,不知道这样算不算unit test : #include : using namespace std; : int BinarySearch(int arr[], int size, int target){ : int high=size-1; : int low=0; : int mid;
| A*****i 发帖数: 3587 | 3 有一种东西叫做test driving programming
每次写代码前先写好test case,然后在开始写代码 | f**********s 发帖数: 115 | 4 lz貌似问的就是怎么想test case啊
有好多种方法决定test case应该是什么, 比如要cover boundary, index off by 1之
类好多细节,除此之外还要考虑if else全部的branch coverage etc
有的时候你感觉全部boundary都cover了, 但是去改一下你的loop故意改错,test居然
还能过,就说明test写的不好。有些工具可以自动篡改你的code来验证test, 这种叫pi
test
要是想系统的学,推荐一本书fundation of software testing, by Aditya Mathur | a******e 发帖数: 124 | 5 对test framework还不太了解,去研究一下
【在 C***y 的大作中提到】 : 找一个unit test framework。基本上就是写各种test case作为参数调用要测的函数, : 然后拿输出结果和预期的正确结果作比较,不一致的话,test就通过不了
| a******e 发帖数: 124 | 6 还没有太多software development经验,每次测试都是在main函数里简单测试一下。。。
谢谢,以后也提前写test case
【在 A*****i 的大作中提到】 : 有一种东西叫做test driving programming : 每次写代码前先写好test case,然后在开始写代码
| a******e 发帖数: 124 | 7 我主要想知道unit test有没有固定格式,比如有没有固定的library method, 我看有
些人用assert,
还有就是你说的这个怎么样想test case, 你说的这些很有启发性,多谢指点
pi
【在 f**********s 的大作中提到】 : lz貌似问的就是怎么想test case啊 : 有好多种方法决定test case应该是什么, 比如要cover boundary, index off by 1之 : 类好多细节,除此之外还要考虑if else全部的branch coverage etc : 有的时候你感觉全部boundary都cover了, 但是去改一下你的loop故意改错,test居然 : 还能过,就说明test写的不好。有些工具可以自动篡改你的code来验证test, 这种叫pi : test : 要是想系统的学,推荐一本书fundation of software testing, by Aditya Mathur
|
|