适用于Visual Studio代码的Windows中的c的Google测试安装指南

Hey, budding c++ coders out there! On your way to becoming a fleshed out developer? I presume you have installed a code editor of your choice, installed compilers, debuggers and all the necessary libraries. But you are missing one mostly overlooked component by beginners, the testing tool.

^ h安永,出芽C ++程序员有! 在成为一名充实的开发人员的路上? 我认为您已经安装了自己选择的代码编辑器,已安装的编译器,调试器和所有必需的库。 但是您缺少初学者最容易忽略的组件,即测试工具。

We will see :

我们会看到 :

1.什么是Google C ++测试框架? (1. What is Google C++ testing framework?)

2.为什么要进行Google测试? (2. Why Google test?)

3.安装步骤 (3. Installation steps)

4.示例程序(4. Sample program)

1.什么是Google C ++测试框架?(1. What is Google C++ testing framework?)

Google Test is a unit testing library for the C++ programming language, based on the xUnit architecture.

Google Test是基于xUnit架构的C ++编程语言的单元测试库。

Or simply put, it makes your C++ testing easy and efficient. Basically, you’ll write a test program, containing many test cases and will check it against your program after compiling Google test into a library.

或简单地说,它使您的C ++测试变得简单而高效。 基本上,您将编写一个包含许多测试用例的测试程序,并将Google测试编译到库中后,将根据您的程序对其进行检查。

The test function is made up of macros, which are more like functions whose parameters are the values inputted to test the function. These macros are assertions which will return True or False according to the assertion type.

测试函数由宏组成,这些宏更像是函数,其参数是为测试该函数而输入的值。 这些宏是断言,将根据断言类型返回True或False。

2.为什么要进行Google测试? (2. Why Google test?)

It is portable and reusable across different platforms. It doesn’t terminate if one test case fails. But it will halt that particular test case and will continue testing other cases. There are 2 kinds of assertions provided by it: i) fatal ii) non-fatal. Moreover, related tests can be grouped into different cases. And each test is run on different objects.

它是可移植的,并且可以在不同平台上重复使用。 如果一个测试用例失败,它不会终止。 但是它将停止该特定测试用例,并将继续测试其他用例。 它提供2种断言:i)致命ii)非致命。 此外,相关的测试可以分为不同的情况。 每个测试都在不同的对象上运行。

3.安装步骤 (3. Installation steps)

  1. This Github repo contains libraries for google test. You can download the version of your choice.

    这个Github存储库包含用于Google测试的库。 您可以下载您选择的版本。

  2. Download libgtest.a and libgtest_main.a libraries.

    下载 libgtest.alibgtest_main.a库。

  3. Copy both these files into lib of MingW (Ex : C:\Program Files\mingw-w64\x86_64–8.1.0-win32-seh-rt_v6-rev0\mingw64\lib)

    将这两个文件都复制到MingW的lib中(例如: C:\ Program Files \ mingw-w64 \ x86_64–8.1.0-win32-seh-rt_v6-rev0 \ mingw64 \ lib )

  4. Go to Google test downloaded repo, extract it and navigate to: googletest →include →gtest [ex C:\Users\Downloads\googletest-release-1.10.0\googletest-release-1.10.0\googletest\include\gtest]. Copy that whole gtest file and copy to the folder MingW\lib\gcc\x86_64-w64-mingw32\8.1.0\include.

    转到Google测试下载的存储库,解压缩并导航至:googletest→包括→gtest [例如C:\ Users \ Downloads \ googletest-release-1.10.0 \ googletest-release-1.10.0 \ googletest \ include \ gtest ]。 复制整个gtest文件并将其复制到文件夹MingW \ lib \ gcc \ x86_64-w64-mingw32 \ 8.1.0 \ include。

大家都准备好编码了! (You are all set to code!)

4.示例程序 (4. Sample program)

Open your Vs Code and let’s start coding. We will need 2 files, one, containing the function which is to be tested and another the test file itself.

打开您的Vs Code,让我们开始编码。 我们将需要2个文件,一个文件包含要测试的功能,另一个文件包含测试文件本身。

Image for post
triangle.h file
triangle.h文件

Here I first created a header file with the function declaration. Then I created another file, triangle.cpp with the function definition.

在这里,我首先使用函数声明创建了头文件。 然后,我创建了另一个带有函数定义的文件triangle.cpp。

Image for post
triangle.cpp
三角形

This is a simple function which 1,2 & 3 respectively for an equilateral, isosceles and scalene triangle.

这是一个简单函数,对于等边,等腰和斜角三角形分别为1,2和3。

Image for post
triangle_test.cpp
triangle_test.cpp

Next is the process of creating the test program, triangle_test. Then we will compile both these files.

接下来是创建测试程序triangle_test的过程。 然后,我们将编译这两个文件。

>> g++ triangle.cpp -c
>> g++ triangle_test.cpp -c
>> g++ triangle.o traingle_test.o -lgtest -lgtest_main -lpthread

Run the executable file at the end and here you go!

最后运行可执行文件,然后运行!

Image for post

PS: If you are a beginner who is into NLP, please give this a read! Check out my other blogs too..

PS:如果你是一个初学者谁把NLP,请给这个一读! 也可以查看我的其他博客

翻译自: https://medium.com/swlh/google-test-installation-guide-for-c-in-windows-for-visual-studio-code-2b2e66352456

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值