1.四种模式输出
模式 |
平台 |
结果输出方式 |
使用的接口函数 |
Basic |
所有 |
标准输出 |
#include "CUnit/Basic.h" CU_basic_set_mode(CU_BRM_VERBOSE); CU_basic_run_tests(); |
Automated |
所有 |
xml文件 |
#include "CUnit/Automated.h" CU_list_tests_to_file(); CU_automated_run_tests(); |
Console |
所有 |
交互式控制台 |
#include "CUnit/Console.h" CU_console_run_tests(); |
Curses |
Linux/Unix |
交互式curses窗口 |
#include "CUnit/CUCurses.h" CU_curses_run_tests(); |
模式 |
介绍 |
Basic |
最常用的,结果输出到标准输出(stdout) |
Automated |
生成完XML文件之后,然后再将CUnit-List.dtd、CUnit-List.xsl、CUnit-Run.dtd、CUnit-Run.xsl(这几个文件在CUnit的源码包可以找到)和XML文件放到同一级目录,再用IE浏览器打开,就可以看到漂亮的界面了。 |
Console |
比较灵活,可以选择只执行其中某一个测试用例。 |
Curses |
跟Console类似,只不过是以Curses窗口的方式展示。 |
2. 断言(#include <CUnit/CUnit.h>)
断言 |
含义 |
CU_ASSERT(int expression) CU_ASSERT_FATAL(int expression) CU_TEST(int expression) CU_TEST_FATAL(int expression) |
断言表达式为TRUE(非零) |
CU_ASSERT_TRUE(value) CU_ASSERT_TRUE_FATAL(value) |
断言值为真(非零) |
CU_ASSERT_FALSE(value) CU_ASSERT_FALSE_FATAL(value) |
断言值为假(零) |
CU_ASSERT_EQUAL(actual, expected) CU_ASSERT_EQUAL_FATAL(actual, expected) |
断言实际值=期望值 |
CU_ASSERT_NOT_EQUAL(actual, expected)) CU_ASSERT_NOT_EQUAL_FATAL(actual, expected) |
断言实际值!=期望值 |
CU_ASSERT_PTR_EQUAL(actual, expected) CU_ASSERT_PTR_EQUAL_FATAL(actual, expected) |
断言指针实际==期待 |
CU_ASSERT_PTR_NOT_EQUAL(actual, expected) CU_ASSERT_PTR_NOT_EQUAL_FATAL(actual, expected) |
断言指针实际!=期待 |
CU_ASSERT_PTR_NULL(value) CU_ASSERT_PTR_NULL_FATAL(value) |
指针值==NULL |
CU_ASSERT_PTR_NOT_NULL(value) CU_ASSERT_PTR_NOT_NULL_FATAL(value) |
指针值!=NULL |
CU_ASSERT_STRING_EQUAL(actual, expected) CU_ASSERT_STRING_EQUAL_FATAL(actual, expected) |
断言实际字符串与预期字符串相等 |
CU_ASSERT_STRING_NOT_EQUAL(actual, expected) CU_ASSERT_STRING_NOT_EQUAL_FATAL(actual, expected) |
断言实际字符串与预期字符串不等 |
CU_ASSERT_NSTRING_EQUAL(actual, expected, count) CU_ASSERT_NSTRING_EQUAL_FATAL(actual, expected, count) |
断言实际和预期的第一个计数字符相同 |
CU_ASSERT_NSTRING_NOT_EQUAL(actual, expected, count) CU_ASSERT_NSTRING_NOT_EQUAL_FATAL(actual, expected, count) |
断言实际和预期的第一个计数字符不同 |
CU_ASSERT_DOUBLE_EQUAL(actual, expected, granularity) CU_ASSERT_DOUBLE_EQUAL_FATAL(actual, expected, granularity) |
断言(实际-预期)<=(粒度)
此断言必须链接到数学库。 |
CU_ASSERT_DOUBLE_NOT_EQUAL(actual, expected, granularity) CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL(actual, expected, granularity) |
断言(实际-预期)>(粒度)
此断言必须链接到数学库。 |
CU_PASS(message) |
用指定的消息注册传递断言。不执行逻辑测试。 |
CU_FAIL(message) CU_FAIL_FATAL(message) |
用指定的消息注册失败的断言。不执行逻辑测试。 |
3 框架及语句分析
语句 |
含义 |
void test_XXX(void) |
testcase |
int suite_init(void) |
在所有testcase运行前执行 |
int suite_clean(void) |
在所有testcase运行后执行 |
void suite_setup(void) |
在每个testcase运行前执行 |
void suite_teardown(void) |
在每个testcase运行后执行 |
CU_SuiteInfo suites[] = { {"suite 1", suite_init, suite_clean, suite_setup, suite_teardown, tests}, CU_SUITE_INFO_NULL }; |
设置Suite信息 |
CU_TestInfo tests[] = { {"test 1", test_process_1 }, {"test 2", test_process_2 }, {"test 3", test_process_3 }, {"test 4", test_process_4 }, CU_TEST_INFO_NULL }; |
设置Test信息 |
int main(int argc, char* argv[]) { CU_ErrorCode err; /*错误信息*/
/* 初始化 */ printf("init\n"); err = CU_initialize_registry();/*初始化*/ if(err){ printf("CU_initialize_registry: %d\n", err); return err; }
/* 添加 suites和tests */ printf("add suites and tests\n"); err = CU_register_suites(suites); if(err) { printf("CU_register_suites: %d\n", err); } CU_pTestRegistry reg = CU_get_registry(); printf("CU_get_registry: %d/%d/%u\n", reg->uiNumberOfSuites, reg->uiNumberOfTests, (long)reg->pSuite);
/*设置运行模式 */ printf("run auto\n"); /**** 自动运行模式 *****************/ CU_set_output_filename("TestProcess"); CU_list_tests_to_file(); CU_automated_run_tests(); //************************************/
printf("run basic\n"); /***** 普通运行模式*************/ CU_basic_set_mode(CU_BRM_VERBOSE); CU_basic_run_tests(); //************************************/
/*****Console运行模式**************** CU_console_run_tests(); /************************************/
/* end */ printf("end\n"); CU_cleanup_registry();/*清除注册*/ err = CU_get_error(); if( err ) { printf("error: %d", err); } return err; |
测试主函数 |
4 Automated下的测试报告输出
在使用Automated下会输出TestProcess-Listing.xml和TestProcess-Results.xml文档,把CUNIT项目中的CUnit-List.xsl、CUnit-Run.xsl、CUnit-List.dtd和CUnit-Run.dtd文件(在%CUNIT_HOME%\Share\目录下)。然后用浏览器打开TestProcess-Listing.xml和TestProcess-Results.xml,如图所示。
具体代码见我前面发的“64位Windows 10下如何搭建CUNIT环境”
————————————————————
软件安全测试
https://study.163.com/course/courseMain.htm?courseId=1209779852&share=2&shareId=480000002205486
接口自动化测试
https://study.163.com/course/courseMain.htm?courseId=1209794815&share=2&shareId=480000002205486
DevOps 和Jenkins之DevOps
https://study.163.com/course/courseMain.htm?courseId=1209817844&share=2&shareId=480000002205486
DevOps与Jenkins 2.0之Jenkins
https://study.163.com/course/courseMain.htm?courseId=1209819843&share=2&shareId=480000002205486
Selenium自动化测试
https://study.163.com/course/courseMain.htm?courseId=1209835807&share=2&shareId=480000002205486
性能测试第1季:性能测试基础知识
https://study.163.com/course/courseMain.htm?courseId=1209852815&share=2&shareId=480000002205486
性能测试第2季:LoadRunner12使用
https://study.163.com/course/courseMain.htm?courseId=1209980013&share=2&shareId=480000002205486
性能测试第3季:JMeter工具使用
https://study.163.com/course/courseMain.htm?courseId=1209903814&share=2&shareId=480000002205486
性能测试第4季:监控与调优
https://study.163.com/course/courseMain.htm?courseId=1209959801&share=2&shareId=480000002205486
Django入门
https://study.163.com/course/courseMain.htm?courseId=1210020806&share=2&shareId=480000002205486
啄木鸟顾老师漫谈软件测试
https://study.163.com/course/courseMain.htm?courseId=1209958326&share=2&shareId=480000002205486