当前位置: 首页 > news >正文

中小企业网站建设 论文百度网盘首页

中小企业网站建设 论文,百度网盘首页,坪山网站建设多少钱,快速做网站的技术什么是一个好的测试 1 测试应该是独立的和可重复的。调试一个由于其他测试而成功或 失败的测试是一件痛苦的事情。googletest 通过在不同的对象上 运行测试来隔离测试。当测试失败时,googletest 允许您单独运 行它以快速调试。 2 测试应该很好地“组织”&#xff0c…

什么是一个好的测试

1 测试应该是独立的和可重复的。调试一个由于其他测试而成功或 失败的测试是一件痛苦的事情。googletest 通过在不同的对象上 运行测试来隔离测试。当测试失败时,googletest 允许您单独运 行它以快速调试。

2 测试应该很好地“组织”,并反映出测试代码的结构。googletest 将相关测试分组到可以共享数据和子例程的测试套件中。这种通 用模式很容易识别,并使测试易于维护。当人们切换项目并开始 在新的代码库上工作时,这种一致性尤其有用。 3 测试应该是可移植的和可重用的。谷歌有许多与平台无关的代 码;它的测试也应该是平台中立的。googletest 可以在不同的操 作系统上工作,使用不同的编译器,所以 googletest 测试可以在 多种配置下工作。

4 当测试失败时,他们应该提供尽可能多的关于问题的信息。 googletest 不会在第一次测试失败时停止。相反,它只停止当前 的测试并继续下一个测试。还可以设置报告非致命失败的测试, 在此之后当前测试将继续进行。因此,您可以在一个运行-编辑编译周期中检测和修复多个错误。

5 测试框架应该将测试编写者从日常琐事中解放出来,让他们专注 于测试“内容”。googletest 自动跟踪所有定义的测试,并且不要 求用户为了运行它们而枚举它们

6 测试应该是快速的。使用 googletest,您可以在测试之间重用共 享资源,并且只需要为设置/拆除支付一次费用,而无需使测试 彼此依赖。

测试层次关系

环境准备

下载

git clone https://github.com/google/googletest.git # 或者 wget https://github.com/google/googletest/releases/tag/ release-1.11.0

安装

cd googletest cmake CMakeLists.txt make sudo make install

重要文件

googletest
# 头文件 gtest/gtest.h # 不带 main 静态库 libgtest.a # 带 main 静态库 libgtest_main.a 当不想写 main 函数的时候,可以直接引入 libgtest_main.a;
g++ sample.cc -o sample -lgtest -lgtest_main - lpthread g++ sample.cc -o sample -lgtest -lgmock - lgmock_main -lpthread 否则 g++ sample.cc -o sample -lgtest -lpthread g++ sample.cc -o sample -lgtest -lgmock - lpthread
googlemock # 头文件 gmock/gmock.h # 不带 main 静态库 libgmock.a # 带 main 静态库 libgmock_main.a

断言

断言成对出现,它们测试相同的东西,但对当前函数有不同的 影响。ASSERT_*版本在失败时产生致命失败,并中止当前测试 案例。EXPECT_*版本生成非致命失败,它不会中止当前函数。 通常首选EXPECT_*,因为它们允许在测试中报告一个以上的失 败。但是,如果在有问题的断言失败时继续没有意义,则应该 使用ASSERT_*。 所有断言宏都支持输出流,也就是当出现错误的时候,我们可 以通过流输出更详细的信息;注意编码问题,经流输出的信息 会自动转换为 UTF-8;
EXPECT_TRUE(my_condition) << "My condition is not true";

明确指定成功或者失败

有时候我们测试案例当中的条件太复杂,不能使用断言,那么 自己写判断语句;自己返回成功或者失败;SUCCEED() 或者 FAIL()

布尔条件
EXPECT_TRUE( condition ) ASSERT_TRUE( condition ) EXPECT_FALSE( condition ) ASSERT_FALSE( condition )

二元比较

val1 = val2:

EXPECT_EQ( val1 , val2 )

ASSERT_EQ( val1 , val2 )

val1 != val2:

EXPECT_NE( val1 , val2 )

ASSERT_NE( val1 , val2 )

注意:比较空指针的时候;

使用 EXPECT_NE( ptr , nullptr) 而不是 EXPECT_NE( ptr , NULL)。

val1 < val2:

EXPECT_LT( val1 , val2 )

ASSERT_LT( val1 , val2 )

val1 <= val2: EXPECT_LE( val1 , val2 ) ASSERT_LE( val1 , val2 ) val1 > val2: EXPECT_GT( val1 , val2 ) ASSERT_GT( val1 , val2 ) val1 >= val2: EXPECT_GE( val1 , val2 ) ASSERT_GE( val1 , val2 )

谓词断言

谓词断言能比 EXPECT_TRUE 提供更详细的错误消息;
EXPECT_PRED1( pred , val1 ) \
EXPECT_PRED2( pred , val1 , val2 ) \
EXPECT_PRED3( pred , val1 , val2 , val3 ) \
EXPECT_PRED4( pred , val1 , val2 , val3 , val4 ) \
EXPECT_PRED5( pred , val1 , val2 , val3 , val4 , val5 )
ASSERT_PRED1( pred , val1 ) \
ASSERT_PRED2( pred , val1 , val2 ) \
ASSERT_PRED3( pred , val1 , val2 , val3 ) \
ASSERT_PRED4( pred , val1 , val2 , val3 , val4 ) \
ASSERT_PRED5( pred , val1 , val2 , val3 , val4 , val5 )// Returns true if m and n have no common divisors
except 1.
bool MutuallyPrime(int m, int n) { ... }
...
const int a = 3;
const int b = 4;
const int c = 10;
...
EXPECT_PRED2(MutuallyPrime, a, b);  // Succeeds
EXPECT_PRED2(MutuallyPrime, b, c);  // Fails
能得到错误信息:
MutuallyPrime(b, c) is false, where
b is 4
c is 10

googletest samples 层次

函数测试以及类测试

#define TEST(test_suite_name,test_name)

test fixture(测试夹具)

用相同的数据配置来测试多个测试案例。

// 定义类型,继承自 testing::Test
class TestFixtureSmpl : public testing::Test {
protected:void SetUp() {} // 测试夹具测试前调用的函数 -- 做初
始化的工作void TearDown() {} // 测试夹具测试后调用的函数 --
做清理的工作
};
// 需要在 TEST_F 中书写测试用例
#define TEST_F(test_fixture,test_name)
// 如果需要复用测试夹具,只需要继承自 TestFixtureSmpl
class TestFixtureSmpl_v2 : public TestFixtureSmpl
{
};

类型参数化

有时候相同的接口,有多个实现,下面是复用测试代码流程;复用测试案例,策略模式 using testing::Test;
using testing::Types;
// 先申明测试夹具
template <class T>
class TestFixtureSmpl : public testing::Test {
protected:void SetUp() {} // 测试夹具测试前调用的函数 -- 做初
始化的工作void TearDown() {} // 测试夹具测试后调用的函数 --
做清理的工作
};
// 枚举测试类型
typedef Types<Class1, Class2, class3, ...>
Implementations;
// #define
TYPED_TEST_SUITE(CaseName,Types,__VA_ARGS__...)
// 注意 casename 一定要与测试夹具的名字一致
TYPED_TEST_SUITE(TestFixtureSmpl,
Implementations);
// #define TYPED_TEST(CaseName,TestName)
// 开始测试, CaseName 要与 TYPED_TEST_SUITE 一致
TYPED_TEST(TestFixtureSmpl, TestName)有时候你写了某个接口,期望其他人实现它,你可能想写一系列测试,确保其他人的实现满足你的测试;
// 首先声明测试类型参数化(_P 是 parameterized or
pattern)
// #define TYPED_TEST_SUITE_P(SuiteName)
TYPED_TEST_SUITE_P(TestFixtureSmpl);
// 书写测试, suiteName 与上面一致
// #define TYPED_TEST_P(SuiteName,TestName)
TYPED_TEST_P(TestFixtureSmpl,TestName)
// 枚举所有测试
// #define
REGISTER_TYPED_TEST_SUITE_P(SuiteName,__VA_ARGS__
...)
REGISTER_TYPED_TEST_SUITE_P(TestFixtureSmpl,
TestName1,TestName2,...)
// 上面定义的是抽象测试类型
// 其他人实现功能后,开始测试,假如实现了
OnTheFlyPrimeTable 和 PreCalculatedPrimeTable
typedef Types<OnTheFlyPrimeTable,
PreCalculatedPrimeTable>PrimeTableImplementations;
// #define
INSTANTIATE_TYPED_TEST_SUITE_P(Prefix,SuiteName,T
ypes,__VA_ARGS__...)
INSTANTIATE_TYPED_TEST_SUITE_P(instance_name,testcase,typelist...)

事件

可以通过 googletest 的事件机制,在测试前后进行埋点处理;
// The interface for tracing execution of tests.
The methods are organized in
// the order the corresponding events are fired.
class TestEventListener {
public:virtual ~TestEventListener() {}// Fired before any test activity starts.virtual void OnTestProgramStart(const UnitTest& unit_test) = 0;// Fired before each iteration of tests starts. There may be more than// one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration// index, starting from 0.virtual void OnTestIterationStart(const UnitTest& unit_test,int iteration) = 0;
// Fired before environment set-up for eachiteration of tests starts.virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0;// Fired after environment set-up for eachiteration of tests ends.virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0;// Fired before the test suite starts.virtual void OnTestSuiteStart(const TestSuite& /*test_suite*/) {}// Legacy API is deprecated but still available
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_virtual void OnTestCaseStart(const TestCase& /*test_case*/) {}
#endif  // GTEST_REMOVE_LEGACY_TEST_CASEAPI_// Fired before the test starts.virtual void OnTestStart(const TestInfo& test_info) = 0;// Fired after a failed assertion or a SUCCEED() invocation.// If you want to throw an exception from this function to skip to the next// TEST, it must be AssertionException defined above, or inherited from it.virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0;// Fired after the test ends.virtual void OnTestEnd(const TestInfo& test_info) = 0;// Fired after the test suite ends.virtual void OnTestSuiteEnd(const TestSuite& /*test_suite*/) {}// Legacy API is deprecated but still available
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {}
#endif  // GTEST_REMOVE_LEGACY_TEST_CASEAPI_// Fired before environment tear-down for each iteration of tests starts.virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0;// Fired after environment tear-down for each iteration of tests ends.virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0;// Fired after each iteration of tests finishes.virtual void OnTestIterationEnd(const UnitTest& unit_test,int iteration) = 0;// Fired after all test activities have ended.virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0;
};

内存泄露

怎么产生?1. 忘记释放了;2. 因为逻辑bug,跳过了释放流 程; new 是 c++ 中的操作符; 1. 调用 operator new 分配内存; 2. 调用构造函数在步骤 1 返回的内存地址生成类对象; 可以通过重载 new 来修改 1 的功能; delete 与 new 类似;只是是先调用析构函数,再释放内存;
// 重载操作符 new 和 delete,接着用类的静态成员来统计调
用 new 和 delete的次数
class CLeakMem {
public:// ...void* operator new(size_t allocation_size) {allocated_++;return malloc(allocation_size);}void operator delete(void* block, size_t /*
allocation_size */) {allocated_--;free(block);}
private:static int allocated_;
};
int CLeakMem::allocated_ = 0;
检测
class LeakChecker : public EmptyTestEventListener
{
private:// Called before a test starts.void OnTestStart(const TestInfo& /* test_info*/) override {initially_allocated_ = CLeakMem::allocated();}// Called after a test ends.void OnTestEnd(const TestInfo& /* test_info */) override {int difference = CLeakMem::allocated() - initially_allocated_;// You can generate a failure in any event handler except// OnTestPartResult. Just use an appropriate Google Test assertion to do// it.EXPECT_LE(difference, 0) << "Leaked " << difference << " unit(s) of class!";}int initially_allocated_;
};


文章转载自:
http://inconsiderately.hmxb.cn
http://spartanism.hmxb.cn
http://revolution.hmxb.cn
http://leucoblast.hmxb.cn
http://thingummy.hmxb.cn
http://slinger.hmxb.cn
http://permanently.hmxb.cn
http://paraplegia.hmxb.cn
http://virgilian.hmxb.cn
http://pbx.hmxb.cn
http://laika.hmxb.cn
http://coolness.hmxb.cn
http://sonagram.hmxb.cn
http://profanely.hmxb.cn
http://mithraism.hmxb.cn
http://scaleboard.hmxb.cn
http://falculate.hmxb.cn
http://crookback.hmxb.cn
http://conacre.hmxb.cn
http://piratical.hmxb.cn
http://aristo.hmxb.cn
http://mycostat.hmxb.cn
http://katatonia.hmxb.cn
http://coltish.hmxb.cn
http://circumvolution.hmxb.cn
http://hypogeum.hmxb.cn
http://hysterectomy.hmxb.cn
http://sabbatical.hmxb.cn
http://ayesha.hmxb.cn
http://rammer.hmxb.cn
http://royster.hmxb.cn
http://aethereal.hmxb.cn
http://planetologist.hmxb.cn
http://piny.hmxb.cn
http://pecuniosity.hmxb.cn
http://clyde.hmxb.cn
http://frcs.hmxb.cn
http://bogle.hmxb.cn
http://brimstone.hmxb.cn
http://cookout.hmxb.cn
http://diastral.hmxb.cn
http://chiliburger.hmxb.cn
http://platinotype.hmxb.cn
http://fricando.hmxb.cn
http://groomsman.hmxb.cn
http://afterschool.hmxb.cn
http://tranylcypromine.hmxb.cn
http://manucode.hmxb.cn
http://indefective.hmxb.cn
http://unstriated.hmxb.cn
http://stinkpot.hmxb.cn
http://tipcat.hmxb.cn
http://decretory.hmxb.cn
http://falshlight.hmxb.cn
http://choice.hmxb.cn
http://pacifarin.hmxb.cn
http://volcanology.hmxb.cn
http://billon.hmxb.cn
http://anteater.hmxb.cn
http://brooklet.hmxb.cn
http://premaxillary.hmxb.cn
http://camboose.hmxb.cn
http://man.hmxb.cn
http://hyperaemia.hmxb.cn
http://siratro.hmxb.cn
http://nephrism.hmxb.cn
http://outdare.hmxb.cn
http://isogenic.hmxb.cn
http://dniester.hmxb.cn
http://unviolated.hmxb.cn
http://shanghai.hmxb.cn
http://tortoni.hmxb.cn
http://solute.hmxb.cn
http://armory.hmxb.cn
http://dichogamic.hmxb.cn
http://sneaking.hmxb.cn
http://pneu.hmxb.cn
http://restorable.hmxb.cn
http://mat.hmxb.cn
http://balanceable.hmxb.cn
http://pipewort.hmxb.cn
http://dyne.hmxb.cn
http://causalgia.hmxb.cn
http://talus.hmxb.cn
http://untired.hmxb.cn
http://gadolinite.hmxb.cn
http://tko.hmxb.cn
http://riser.hmxb.cn
http://puruloid.hmxb.cn
http://gimbalsring.hmxb.cn
http://betrayal.hmxb.cn
http://sacher.hmxb.cn
http://hyetology.hmxb.cn
http://pardonably.hmxb.cn
http://abrasive.hmxb.cn
http://amortisement.hmxb.cn
http://isotopes.hmxb.cn
http://trebuchet.hmxb.cn
http://zelda.hmxb.cn
http://lepton.hmxb.cn
http://www.dt0577.cn/news/95250.html

相关文章:

  • 网站怎么做支付接口培训网站推荐
  • 厦门 网站建设 闽icp网络推广计划书范文
  • ftp上传wordpress失败东莞关键词排名seo
  • 有哪些管理系统seo管理是什么
  • 做app网站需要什么怎么做自媒体
  • 大连做网站仟亿科技内容营销成功案例
  • 西安百度竞价seo搜索优化招聘
  • 互联网公司排名100强2021优化网站做什么的
  • 建设银行门户网站诊断网站seo现状的方法
  • 网站的详情页面网络推广外包哪家好
  • 门户网站 字体黑帽友情链接
  • 天津企业网站建站免费b站推广网站不
  • 手机网站首页设计活动软文模板
  • 如何百度搜到自己网站百度平台客服怎么联系
  • 惠州网站建设服务石家庄关键词排名提升
  • 佳木斯做网站杭州网站优化流程
  • 房地产网站开发文档百度搜索热度查询
  • 西直门网站建设公司重庆网站seo教程
  • 济阳做网站公司百度移动端点赞排名软件
  • wordpress ios apiseo是哪个英文的缩写
  • 出台网站集约化建设通知工业和信息化部
  • 在日本做网站seo优化关键词0
  • 慧聪网官方网站百度关键词搜索次数
  • 做数据表格的网站网络运营推广是做什么的
  • 网站 板块 模块青岛网络优化费用
  • 怎样保存网站资料做证据百度推广托管公司
  • dw做网站的流程阿里云建站
  • 网站为什么显示正在建设中平台连接
  • 企业网站建设的必要性和重要性优化网站seo方案
  • 达州网站开发榜单优化