64位Windows 10下如何搭建CUNIT环境?

发表于:2020-6-19 09:58

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:顾翔    来源:51Testing软件测试网原创

分享:
   Windows下如何搭建CUNIT环境资料很多,但是错误不少或者讲解不清晰,很容易让人跌入坑中,现在介绍如下。
  1 安装mingw
  网上多处文章介绍下载mingw-get,由于现在网上大都数mingw-get均为32位的,所以不能用这个。所以大家需要下载https://sourceforge.net/projects/mingw-w64/files/下载mingw-w64-install.exe,在安装的时候注意:
  i686为32位的,x86_64为64位的。所以这里我们选择x86_64。安装完毕配置环境变量。加入MINGW_HOME环境变量,我这里路径为:C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\。在PATH中设置%MinGW_HOME%\mingw64\bin\和%MinGW_HOME%\mingw64\include\。打开cmd,输入gcc,如果返回gcc: fatal error: no input files则说明配置正确。
  2 下载CUNIT tar包
  CUNIT tar包是为Linux开发的,但是在Windows下可用msys2工具进行编译。我解压完毕放在C:\CUnit-2.1-3目录下
  3 安装msys2
  msys2可以让你在Windows下编译Linux的代码,目前网站上有简易版和完全版,简易版下载以后还需要安装各个命令的插件,比较麻烦,所以建议安装完全版,完整版的文件名为msys+7za+wget+svn+git+mercurial+cvs-rev13.7z(这个文件比较大,下载需要1个多小时)。解压完毕请把文件夹msys全部拷贝到mingw的目录下,我这里为C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64。
  进入C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\msys,打开msys.bat。执行以下命令:
   cd C:\CUnit-2.1-3 #解压的CUnit的根目录
  libtoolize
  automake --add-missing
  autoreconf
  ./configure --prefix=/mingw
  make
  make install
  安装完毕把C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\msys\mingw\lib\ libcunit.a拷贝到C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\lib。(不拷贝在下面gcc或者clang运行中,加入-lcunit参数会提示..lib: can’t find -lcunit的错误)
  接下来把C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\msys\mingw\include\CUnit\目录中的所有.h文件拷贝到C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\include中去。
  4 建立测试文件和被测文件
  在任意工程目录下建立被测试文件:process.h和process.c
  process.h:
 extern int process(int x, int y, int z);
  process.c:
   #include <stdio.h>

  int process(int x, int y, int z){
       int k=0;
       int j=0;
       if ((x>3) && (z<10)){
          k=x*y-1;
          j=k^2;
       }
       if((x==4) || (y>5)){
         j=x*j+10;
       }
       j=j%3;
       return k+j;
  }
  和测试文件test_main.c:
   #include "process.h"

  /* test cases */

  void test_process_1(void)
  {
      CU_ASSERT(process(4,6,9)==24);
  }
  void test_process_2(void)
  {
      CU_ASSERT(process(4,5,10)==1);
  }
  void test_process_3(void)
  {
      CU_ASSERT(process(5,4,9)==21);
  }
  void test_process_4(void)
  {
      CU_ASSERT(process(2,5,10)==0);
  }
  CU_TestInfo tests[] = {
  {"test 1", test_process_1 },
  {"test 2", test_process_2 },
  {"test 3", test_process_3 },
  {"test 4", test_process_4 },
      CU_TEST_INFO_NULL
  };
  /* suite init */
  int suite_init(void)
  {
      return 0;
  }
  int suite_clean(void)
  {
      return 0;
  }
  void suite_setup(void)
  {
  }
  void suite_teardown(void)
  {
  }
  CU_SuiteInfo suites[] = {
      {"suite 1", suite_init, suite_clean, suite_setup, suite_teardown, tests},
      CU_SUITE_INFO_NULL
  };
  /* registry */

  int main(int argc, char* argv[])
  {
      CU_ErrorCode err;
  /* init */
  printf("init\n");
  err = CU_initialize_registry();
  if( err ){
  printf("CU_initialize_registry: %d\n", err);
  return err;
  }
  /* add suites and tests */
  printf("add suites and tests\n");
  err = CU_register_suites(suites);
  if( err )
  {
  printf("CU_register_suites: %d\n", err);
  }
  CU_pTestRegistry reg = CU_get_registry();
  printf("CU_get_registry: %d/%d/%u\n", reg->uiNumberOfSuites, reg->uiNumberOfTests, (long)reg->pSuite);
  /* run auto */
  printf("run auto\n");
  /**** Automated Mode *****************/
  CU_set_output_filename("TestProcess");
  CU_list_tests_to_file();
  CU_automated_run_tests();
  //************************************/
  printf("run basic\n");
  /***** Basice Mode *******************/
  CU_basic_set_mode(CU_BRM_VERBOSE);
  CU_basic_run_tests();
  //************************************/
  /*****Console Mode ********************
  CU_console_run_tests();
  //************************************/
  /* end */
  printf("end\n");
  CU_cleanup_registry();
  err = CU_get_error();
  if( err )
  {
      printf("error: %d", err);
    }
    return err;
  }
  通过以下命令编译(可以把这个命令放入一个bat文件中):
  gcc process.c test_main.c -o test -I/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/include -L/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib -lcunit -static
  编译完成出现一个名为test.exe的文件,在cmd中运行它,会产生测试结果。
   C:\MyC\process>test
  init
  add suites and tests
  CU_get_registry: 1/4/1909760
  run auto
  run basic

       CUnit - A unit testing framework for C - Version 2.1-3
       http://cunit.sourceforge.net/
  Suite: suite 1
    Test: test 1 ...passed
    Test: test 2 ...passed
    Test: test 3 ...passed
    Test: test 4 ...passed
  Run Summary:    Type  Total    Ran Passed Failed Inactive
  suites      1      1    n/a      0        0
  tests      4      4      4      0        0
  asserts      4      4      4      0      n/a
  Elapsed time =    0.048 seconds
  end
  5. 使用VS Code IDE
  这里基本上就可以使用CUNIT进行工作了,但是为了调试的方便,我们需要安装一个IDE,这里建议使用微软的VS Code。
  5.1 下载并且安装Visual Studio Code
  5.2 下载并且安装LLVM。在选择三个单选框时选择 第二个选项 Add LLVM to the system PATH for all users。安装完毕在cmd窗口中输入clang,提示 no input file 表示安装成功。
  5.3 打开VS Code
  在插件中输入Chinese,安装重启VS Code变为中文版
  在插件中输入c/c++,选第一个安装
  在插件中输入code runner,选第一个安装
  在插件中输入clang,选第一个安装
  5.4 配置4个json配置文件
  刚才的文件是在C:\MyC\process目录下,在C:\MyC\.vscode下建立四个json文件。
  launch.json
   // https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md
  {
     "version": "0.2.0",
     "configurations": [
       {
          "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
          "type": "cppdbg", // 配置类型,这里只能为cppdbg
          "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
          "program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
          "args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
          "stopAtEntry": true, // 设为true时程序将暂停在程序入口处,我一般设置为true
          "cwd": "${workspaceFolder}", // 调试程序时的工作目录
          "environment": [], // 环境变量
          "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台
          "internalConsoleOptions": "neverOpen", // 如果不设为neverOpen,调试时会跳到“调试控制台”选项卡,你应该不需要对gdb手动输命令吧?
          "MIMode": "gdb", // 指定连接的调试器,可以为gdb或lldb。但我没试过lldb
          "miDebuggerPath": "gdb.exe", // 调试器路径,Windows下后缀不能省略,Linux下则不要
          "setupCommands": [ // 用处未知,模板如此
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": false
            }
         ],
         "preLaunchTask": "Compile" // 调试会话开始前执行的任务,一般为编译程序。与tasks.json的label相对应
      }
    ]
  }
  setting.json
   {
    "files.defaultLanguage": "c", // ctrl+N新建文件后默认的语言
     "editor.formatOnType": true, // 输入时就进行格式化,默认触发字符较少,分号可以触发
    "editor.snippetSuggestions": "top", // snippets代码优先显示补全
    "code-runner.runInTerminal": true, // 设置成false会在“输出”中输出,无法输入
    "code-runner.executorMap": {
    "c": "cd $dir && clang $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics --target=x86_64-w64-mingw -std=c11 && $dir$fileNameWithoutExt",
    "cpp": "cd $dir && clang++ $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics --target=x86_64-w64-mingw -std=c++17 && $dir$fileNameWithoutExt"
  }, // 设置code runner的命令行
     "code-runner.saveFileBeforeRun": true, // run code前保存
     "code-runner.preserveFocus": true, // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false
     "code-runner.clearPreviousOutput": false, // 每次run code前清空属于code runner的终端消息
     "C_Cpp.clang_format_sortIncludes": true, // 格式化时调整include的顺序(按字母排序)
     "C_Cpp.intelliSenseEngine": "Default", // 可以为Default或Tag Parser,后者较老,功能较简单。具体差别参考cpptools扩展文档
     "C_Cpp.errorSquiggles": "Disabled", // 因为有clang的lint,所以关掉
     "C_Cpp.autocomplete": "Disabled", // 因为有clang的补全,所以关掉
     "clang.cflags": [ // 控制c语言静态检测的参数
     "--target=x86_64-w64-mingw",
     "-std=c11",
     "-Wall",
     "-lcunit"
     ],
     "clang.cxxflags": [ // 控制c++静态检测时的参数
     "--target=x86_64-w64-mingw",
     "-std=c++17",
     "-Wall",
     "-lcunit"
     ],
     "clang.completion.enable":true // 效果效果比cpptools要好
     }
  task.json
  // https://code.visualstudio.com/docs/editor/tasks
  {
      "version": "2.0.0",
      "tasks": [
          {
              "label": "Compile", // 任务名称,与launch.json的preLaunchTask相对应
              "command": "clang", // 要使用的编译器,C++用clang++
              "args": [
                  "${file}",
                  "-o", // 指定输出文件名,不加该参数则默认输出a.exe,Linux下默认a.out
                  "${fileDirname}/${fileBasenameNoExtension}.exe",
                  "-g", // 生成和调试有关的信息
                  "-Wall", // 开启额外警告
                  "-static-libgcc", // 静态链接libgcc
                  "--target=x86_64-w64-mingw", // clang的默认target为msvc,不加这一条就会找不到头文件;Linux下去掉这一条
                  "-std=c11" ,// C++最新标准为c++17,或根据自己的需要进行修改
              ], // 编译命令参数
              "type": "shell", // 可以为shell或process,前者相当于先打开shell再输入命令,后者是直接运行命令
              "group": {
                  "kind": "build",
                  "isDefault": true // 设为false可做到一个tasks.json配置多个编译指令,需要自己修改本文件,我这里不多提
              },
              "presentation": {
                  "echo": true,
                  "reveal": "always", // 在“终端”中显示编译信息的策略,可以为always,silent,never。具体参见VSC的文档
                  "focus": false, // 设为true后可以使执行task时焦点聚集在终端,但对编译c和c++来说,设为true没有意义
                  "panel": "shared" // 不同的文件的编译信息共享一个终端面板
              }
              // "problemMatcher":"$gcc" // 如果你不使用clang,去掉前面的注释符,并在上一条之后加个逗号。照着我的教程做的不需要改(也可以把这行删去)
          }
      ]
  }   
     c_cpp_properties.json
   {
     "configurations": [
       {
          "name": "MinGW",
          "intelliSenseMode": "gcc-x64",
          "compilerPath": "C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc.exe",
               "includePath": [
                   "${workspaceFolder}"
                 ],
               "defines": [],
               "browse": {
                   "path": [
                      "${workspaceFolder}"
                    ],
                    "limitSymbolsToIncludedHeaders": true,
                    "databaseFilename": ""
                },
               "cStandard": "c11",
               "cppStandard": "c++17"
          }
      ],
      "version": 4
  }
  就可以使用这个工具运行C程序了。但是运行CUNIT程序请在CMD中运行。

        版权声明:本文出自51Testing会员投稿,51Testing软件测试网及相关内容提供者拥有内容的全部版权,未经明确的书面许可,任何人或单位不得对本网站内容复制、转载或进行镜像,否则将追究法律责任。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号