Boost.Test 夹具的使用
夹具,特别像:一个汉堡包,上面一层,下面一层!
中间夹着相应的测试
级别:
- 全局夹具
- 测试套件夹具
- 测试用例夹具
可以说,是:十分的合理了
用例级别的夹具:
#ifndef BOOSTLEARN_TEST3_H
#define BOOSTLEARN_TEST3_H
#include"boost/test/unit_test.hpp"
#include<iostream>
#include<string>
struct fixture_case1{
fixture_case1(){
i=0;
name="";
std::cout<<"夹具开始"<<std::endl;
};
int i;
std::string name;
~fixture_case1(){
std::cout<<"夹具结束"<<std::endl;
}
};
BOOST_AUTO_TEST_SUITE( s_suit3 )
BOOST_FIXTURE_TEST_CASE( t_case1,fixture_case1 )
{
BOOST_CHECK_EQUAL(1,3);
}
BOOST_AUTO_TEST_CASE( t_case2 )
{
BOOST_CHECK_EQUAL(1,1);
}
BOOST_AUTO_TEST_SUITE_END()
#endif//BOOSTLEARN_TEST3_H
测试套件夹具
#ifndef BOOSTLEARN_TEST3_H
#define BOOSTLEARN_TEST3_H
#include"boost/test/unit_test.hpp"
#include<iostream>
#include<string>
struct fixture_case1{
fixture_case1(){
i=0;
name="case1";
std::cout<<"case1夹具开始"<<std::endl;
};
int i;
std::string name;
~fixture_case1(){
std::cout<<"case1夹具结束"<<std::endl;
}
};
struct fixture_suit1{
fixture_suit1(){
i=0;
name="suit1";
std::cout<<"suit1夹具开始"<<std::endl;
};
int i;
std::string name;
~fixture_suit1(){
std::cout<<"suit1夹具结束"<<std::endl;
}
};
BOOST_FIXTURE_TEST_SUITE( s_suit3,fixture_suit1 )
BOOST_FIXTURE_TEST_CASE( t_case1,fixture_case1 )
{
BOOST_CHECK_EQUAL(1,3);
name="徐锦涛";
BOOST_CHECK_MESSAGE(1==2,"错误:"+name);
}
BOOST_AUTO_TEST_CASE( t_case2 )
{
BOOST_CHECK_EQUAL(1,1);
BOOST_CHECK_MESSAGE(1==2,"错误:"+name);
}
BOOST_AUTO_TEST_SUITE_END()
#endif//BOOSTLEARN_TEST3_H
输出结果:
Running 7 test cases...
case1夹具开始
/Users/guabutian/Documents/codeWorld/CandC++/C/BoostLearn/src/test/test3.h:41: error: in "s_suit3/t_case1": check 1 == 3 has failed [1 != 3]
/Users/guabutian/Documents/codeWorld/CandC++/C/BoostLearn/src/test/test3.h:43: error: in "s_suit3/t_case1": 错误:徐锦涛
case1夹具结束
suit1夹具开始
/Users/guabutian/Documents/codeWorld/CandC++/C/BoostLearn/src/test/test3.h:49: error: in "s_suit3/t_case2": 错误:suit1
suit1夹具结束
*** 3 failures are detected in the test module "TestUnit"
可以看到:
除了全局夹具之外,其他的夹具都是作用到case上的,而且,case里的夹具会掩盖suit的夹具
全局夹具,则是给整个测试模块加的夹具
//
// Created by 瓜不甜 on 2018/1/17.
//
#ifndef BOOSTLEARN_TEST3_H
#define BOOSTLEARN_TEST3_H
#include"boost/test/unit_test.hpp"
#include<iostream>
#include<string>
struct fixture_case1{
fixture_case1(){
i=0;
name="case1";
std::cout<<"case1夹具开始"<<std::endl;
};
int i;
std::string name;
~fixture_case1(){
std::cout<<"case1夹具结束"<<std::endl;
}
};
struct fixture_suit1{
fixture_suit1(){
i=0;
name="suit1";
std::cout<<"suit1夹具开始"<<std::endl;
};
int i;
std::string name;
~fixture_suit1(){
std::cout<<"suit1夹具结束"<<std::endl;
}
};
struct fixture{
fixture(){
i=0;
gname="suit1";
std::cout<<"全局夹具开始"<<std::endl;
};
int i;
std::string gname;
~fixture(){
std::cout<<"全局夹具结束"<<std::endl;
}
};
// 定义全局夹具
BOOST_GLOBAL_FIXTURE(fixture);
BOOST_FIXTURE_TEST_SUITE( s_suit3,fixture_suit1 )
BOOST_FIXTURE_TEST_CASE( t_case1,fixture_case1 )
{
BOOST_CHECK_EQUAL(1,3);
name="徐锦涛";
BOOST_CHECK_MESSAGE(1==2,"错误:"+name);
}
BOOST_AUTO_TEST_CASE( t_case2 )
{
BOOST_CHECK_EQUAL(1,1);
BOOST_CHECK_MESSAGE(1==2,"错误:"+name);
}
BOOST_AUTO_TEST_CASE( t_case3 )
{
BOOST_CHECK_EQUAL(1,1);
}
BOOST_AUTO_TEST_SUITE_END()
#endif//BOOSTLEARN_TEST3_H
但,在我这里发现了个问题:全局夹具的成员变量貌似没有办法在case里使用,我觉得,有空要研究下源码,这样的话,才可以,完全掌握这个东西