具有LinkTime替换的C ++ unitTest [英] C++ unitTest with LinkTime substitution

查看:102
本文介绍了具有LinkTime替换的C ++ unitTest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读James W. Grenning的嵌入式C的测试驱动开发".

I'm reading "Test-Driven Development for Embedded C," by James W. Grenning.

我想使用Visual Studio Community 2019和gTest通过链接时替换"来复制大小写.

I'd like to reproduce case with "Link-Time Substitution" using Visual Studio Community 2019 and gTest.

我有以下代码:

production_code静态链接库

production_code staticly linked library

foo.cpp

#include "foo.h"

int foo(int x) {
    return x + 1;
}

foo.h

#ifndef _foo_
#define _foo_

int foo(int x);

#endif //_foo_

在gtest项目中,production_code库通过引用包括在内 test.cpp

In gtest project production_code library is included via reference test.cpp

#include "gtest\gtest.h"
#include "gmock\gmock.h"

#include "..\prod\foo.h"

//fake implementation of production code foo
int foo(int x) {
    return x - 1;
}
TEST(TestCaseName, TestName) {
  auto x = foo(5);
  EXPECT_EQ(x, 4);
}

链接器给我以下错误:

1> prod.lib(foo.obj):错误LNK2005:"int __cdecl foo(int)" 已在test.obj 1> C:\ Example \ prod_test.exe中定义(?foo @@ YAHH @ Z) :致命错误LNK1169:找到一个或多个乘法定义的符号

1>prod.lib(foo.obj) : error LNK2005: "int __cdecl foo(int)" (?foo@@YAHH@Z) already defined in test.obj 1>C:\Example\prod_test.exe : fatal error LNK1169: one or more multiply defined symbols found

我在这里错过了什么?为什么这行不通?

What have I missed here? Why doesn't this work?

如果我在链接器中添加命令"/FORCE:MULTIPLE",则只会收到警告,但我认为这不是正确的方法.

If I add the command "/FORCE:MULTIPLE" to linker, then I get only warning, but I think that this is not the right approach to doing this.

推荐答案

之所以会发生这种情况,是因为您在2个地方定义了foo(int): foo.cpp test.cpp ,然后使用此文件构建代码.如果您需要使用 stub (在这种情况下为伪函数foo(int))进行测试您需要为以下对象创建2个构建目标:

This situation occurs because you defined foo(int) in 2 places: foo.cpp and test.cpp and you build your code with this files. If you need to run some test with a stub (fake function foo(int) in this case) you need to create 2 targets of build for:

  1. 构建您的真实应用程序
  2. 构建用于单元测试(然后运行)的特殊应用程序

在构建用于单元测试的应用程序时,将其与 test.cpp 链接(但不与 foo.cpp 链接).同样,在构建实际应用程序时,您可以将其与 foo.cpp 链接(但不能与 test.cpp 链接).

And when you build the application for unit testing, you link it with test.cpp (but not with foo.cpp). Also when you build your real application you link it with foo.cpp (but not with test.cpp).

注意:有意义的是,测试实际代码并创建存根时,该存根根据测试的思想提供了一些其他功能(例如,您检查了sort()函数,但您可以使用存根为sort()生成数据,因为数据是在某些复杂算法完成工作之后才提供的,并且计算需要大量时间),或者您不想使用某些资源(例如,检查sort()函数但是您使用与服务器的网络连接来获取sort()的真实数据),或者需要提供一些特定的数据来测试算法(例如,检查

Note: it's make sense to test real code and create a stub when this stub provides some additional functionality according to idea of a test (for example, you check your sort() function but you can use stub to generate data for sort() because the data are provided after some complex algorithm finished working and the computing takes a lot of time) or you don't want to use some resources (for example, you check your sort() function but you use a network connection to a server to get real data for sort()) or you need to provide some specific data for testing you algorithm (for example, to check a corner case using specific data; or maybe you have found the sort() doesn't work with specific data). But again, it's make sense to test real code.

这篇关于具有LinkTime替换的C ++ unitTest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆