简体   繁体   English

通过对常用类的测试类来测试C ++程序

[英]Testing C++ program with Testing classes over normally used classes

This will probably be a bot of a waffly question but ill try my best. 这可能是一个棘手的问题的机器人,但我会尽力而为。

I have a simple c++ program that i need to build testing for. 我有一个简单的c ++程序,需要对其进行测试。 I have 2 Classes i use besides the one i actually am using, these are called WebServer and BusinessLogicLayer. 除了实际使用的类外,我还有2个类,它们称为WebServer和BusinessLogicLayer。

To test my own code i have made my own versions of these classes that feed dummy data to my class to test it functionality. 为了测试自己的代码,我制作了这些类的自己的版本,这些版本将伪数据馈送到我的类中以测试其功能。

I need to know a way of somehow, via a makefile for instance, how to tell the source code to use the test classes over the normally used classes. 我需要通过某种方式(例如通过makefile)来了解某种方式,该方式如何告诉源代码使用测试类而不是通常使用的类。 The test classes are in a different "tester" c++ file, and the tester c++ file also has its own header file. 测试类位于另一个“测试器” c ++文件中,并且测试器c ++文件也具有其自己的头文件。

Regards 问候

Paul 保罗

PS This is probably a badly worded question, but i dont know any better way to put my question. PS:这可能是措辞不佳的问题,但是我不知道有什么更好的方式提出我的问题。

You can define abstract base classes that declare the public interfaces for your components, then wire the objects together at runtime (in main() or something else fairly high up in the food chain). 您可以定义抽象基类,为您的组件声明公共接口,然后在运行时将对象连接在一起(在main()或食物链中相当重要的其他位置)。 In testing, you simply wire up different objects. 在测试中,您只需连接不同的对象。

To build debug and release versions of a program with source code in directories ${SRC_DIR_1} and ${SRC_DIR_2}: 要在目录$ {SRC_DIR_1}和$ {SRC_DIR_2}中使用源代码构建程序的调试和发布版本:

CXX      := g++
CPPFLAGS  = ...
CXXFLAGS  = ...

SRC_DIR_1 := ...
SRC_DIR_2 := ...

ifeq (${debug},1)
  BIN_DIR  := ./obj_debug
  CXXFLAGS += -g
else
  BIN_DIR  := ./obj_release
  CXXFLAGS += -DNDEBUG
endif

# Make sure that the directory exists.
TEMP := ${shell test -d ${BIN_DIR} || mkdir ${BIN_DIR}}

# Tell make to search each directory
VPATH := ${SRC_DIR_1}:${SRC_DIR_2}

# You can modify this to use a suffix other than .cc
${BIN_DIR}/%.o : %.cc
    ${CXX} ${CPPFLAGS} ${CXXFLAGS} -c $< -o $@

# Build the requested version of the program.
ifeq (${debug},1)
  default: program_name_debug
else
  default: program_name
endif

tidy::
    @${RM} -r ./obj_*

In the Project Configuration dialog, specify the target name as "program_name, program_name_debug". 在“项目配置”对话框中,将目标名称指定为“ program_name,program_name_debug”。 (Replace program_name with the name of your program.) (将program_name替换为您的程序的名称。)

To build the program, invoke "make debug=X" with X replaced by either 0 or 1. 要生成程序,请调用“ make debug = X”,将X替换为0或1。

Reference 参考

Why does your tester code have a header file of its own? 为什么您的测试人员代码具有自己的头文件? Your test code should have the same interface as the code it emulates, and using the same header file prevents a lot of headaches. 您的测试代码应具有与其仿真代码相同的接口,并且使用相同的头文件可以避免很多麻烦。

Anyway, this will work: 无论如何,这将起作用:

real_program: WebServer.o BusinessLogicLayer.o

test_program: tester.o

real_program test_program: MyClass.o OtherThings.o
    $(LINK) $^ -o $@

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM