我们来看一个简单的C程序:

 

#include <stdio.h>
#include <string.h>

int isGoodString(const char *p)
{
	if(strstr(p, "Good"))
	{
		return 0;
	}
	
	return -1;
}

int main(int argc, char *argv[])
{
	if(2 != argc)
	{
		printf("para error\n");
		return -1;
	}
	
	if(0 == isGoodString(argv[1]))
	{
		printf("yes\n");
		return 0;
	}
	
	printf("no\n");
	return 1;

}

      现在, 我们来测试一下:

 

 

[taoge@localhost learn_c]$ gcc test.c 
[taoge@localhost learn_c]$ ./a.out 
para error
[taoge@localhost learn_c]$ ./a.out 12
no
[taoge@localhost learn_c]$ ./a.out good
no
[taoge@localhost learn_c]$ ./a.out Good123
yes
[taoge@localhost learn_c]$ ./a.out Good
yes
[taoge@localhost learn_c]$ 


      我们可以看到, 这些测试用例是手动的。 手动测试用例的缺点是:

 

      1. 手动测试用例不便于保存(假设一个月后, 要再测一遍, 还得再敲一次。 当然, 你可能说, 你会保存这些文本, 但那样也需要复制命令到shell中重新运行)

      2. 手动测试用例很麻烦, 稍微不注意就会出错,没有一气呵成的感觉,  不利于自动化测试。

 

      对了, 前面不是一直在说脚本脚本么, 现在用脚本来搞一下自动化测试用例:

 

#!/bin/sh

$1
echo ""

x=good
echo "$x"
$1 "$x"

x=goodbye
echo "$x"
$1 "$x"

x=Good
echo "$x"
$1 "$x"

x=Goodbye
echo "$x"
$1 "$x"

x="Good bye"
echo "$x"
$1 "$x"


    结果为:

 

 

[taoge@localhost learn_c]$ ls
a.out  test.c  test.sh
[taoge@localhost learn_c]$ ./test.sh ./a.out 
para error

good
no
goodbye
no
Good
yes
Goodbye
yes
Good bye
yes
[taoge@localhost learn_c]$ 


      自动化, 真是好啊, 一劳永逸。

 

 

 

 


本文转载:CSDN博客