通过前几期的构建,我们已经可以在jenkins中对一个单独的exe执行文件进行测试并生成覆盖路报告。但是有两个问题:
1、鉴于SVN管理源码,我们不可能每次要求用户将exe执行文件上传,如何能够在用户上传源码以后也能在远程实现重新的编译生成exe执行文件呢?
2、如果用户需要在job中可以同时构建多个工程,该如何实现?
3、如果用户上传了新的工程,如何在构建时自动检测并同以前的工程一起生成测试报告?
鉴于上面的问题,我们一一作出解答:
1、在远程执行源码的编译,只能是在用户点击“构建”时,通过Dos命令执行编译,对,就是我们熟悉的qmake。
2、在同一个job中构建多个工程,我们可以Dos命令中连续openCppCoverage.exe多个执行程序
3、对于新工程的检测,目前没有想到好的办法,自己写了一个检测程序,可以检测当前目录下所有的.pro文件,并生成对应的Dos命令,并执行。
具体实现内容如下:
首先将自己写的检测.pro程序编译成exe执行文件并在job中配置:
程序源码如下:
#include "gsearchfile.h"
#include <QDebug>
#include <QProcess>
#include <QDir>
#include <QDirIterator>
GSearchFile::GSearchFile(QObject *parent) :
QObject(parent)
{
searchFiel("F:\\QTWorkSpace\\TEST");
QProcess p;
p.start("cmd.exe", QStringList() << "/c" << "F:\\QTWorkSpace\\searchFile\\search.bat");
if (p.waitForStarted())
{
p.waitForFinished();
qDebug() << p.readAllStandardOutput();
}
else
qDebug() << "Failed to start";
}
GSearchFile::~GSearchFile()
{
}
/*
*遍历path路径下的所有子文件,找出.pro工程文件并记录其路径
*编写.bat脚本
*
*/
void GSearchFile::searchFiel(QString path)
{
//判断路径是否存在
QDir dir(path);
if(!dir.exists())
{
return;
}
//获取所选文件类型过滤器
QStringList filters;
filters<<QString("*.pro");
//定义迭代器并设置过滤器
QDirIterator dir_iterator(path,
filters,
QDir::Files | QDir::NoSymLinks,
QDirIterator::Subdirectories);
QStringList string_debugPath;//debug路径
QStringList string_file;//文件名
QStringList string_fileEXE;//exe执行文件名
QStringList string_project;//.pro工程名
QStringList string_projectPath;//.pro工程路径
QStringList string_projectNativePath;//将qt路径转换为适合应用平台的路径
while(dir_iterator.hasNext())
{
dir_iterator.next();
QFileInfo file_info = dir_iterator.fileInfo();
QString strProject=file_info.fileName();
QString strfile = file_info.baseName();
QString strProjectPath=file_info.absolutePath();
string_debugPath.append(strProjectPath+"/debug");
string_file.append(strfile+"Coverage.xml");
string_fileEXE.append(strfile+".exe");
string_project.append(strProject);
string_projectPath.append(strProjectPath);
string_projectNativePath.append(QDir::toNativeSeparators(strProjectPath));//将qt路径转化为本地路径并保存
}
//删除已经存在的search.bat
QString reFileName="F:/QTWorkSpace/searchFile/search.bat";
QFile reFile;
reFile.remove(reFileName);
//创建新的search.bat
QString fileName="F:/QTWorkSpace/searchFile/search.bat";
QFile file(fileName);
if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
{
return ;
}
//将命令行写入.bat文件
QTextStream in(&file);
in<<"cd /d C:\\Users\\handh\\.jenkins\\workspace\\unittest4"<<"\n";
for(int i=0;i<string_file.size();i++)
{
in<<"del "+string_file.at(i)<<"\n";
}
for(int i=0;i<string_file.size();i++)
{
in<<"cd /d "+string_projectPath.at(i)<<"\n";
in<<"qmake "+string_project.at(i)<<"\n";
in<<"jom.exe /f Makefile.Debug"<<"\n";
in<<"cd /d "+string_debugPath.at(i)<<"\n";
in<<"OpenCppCoverage.exe --sources "+string_projectNativePath.at(i);
in<<" --export_type=cobertura:C:\\Users\\handh\\.jenkins\\workspace\\unittest4\\"+string_file.at(i);
in<<" -- "+string_fileEXE.at(i)<<"\n";
}
file.close();
}
当程序检索完当前目录后会生成一个search.bat的脚本文件,具体内容如下:
cd /d C:\Users\handh\.jenkins\workspace\unittest4
del test1Coverage.xml
del test2Coverage.xml
del test3Coverage.xml
cd /d F:/QTWorkSpace/TEST/test1
qmake test1.pro
jom.exe /f Makefile.Debug
cd /d F:/QTWorkSpace/TEST/test1/debug
OpenCppCoverage.exe --sources F:\QTWorkSpace\TEST\test1 --export_type=cobertura:C:\Users\handh\.jenkins\workspace\unittest4\test1Coverage.xml -- test1.exe
cd /d F:/QTWorkSpace/TEST/test2
qmake test2.pro
jom.exe /f Makefile.Debug
cd /d F:/QTWorkSpace/TEST/test2/debug
OpenCppCoverage.exe --sources F:\QTWorkSpace\TEST\test2 --export_type=cobertura:C:\Users\handh\.jenkins\workspace\unittest4\test2Coverage.xml -- test2.exe
cd /d F:/QTWorkSpace/TEST/test3
qmake test3.pro
jom.exe /f Makefile.Debug
cd /d F:/QTWorkSpace/TEST/test3/debug
OpenCppCoverage.exe --sources F:\QTWorkSpace\TEST\test3 --export_type=cobertura:C:\Users\handh\.jenkins\workspace\unittest4\test3Coverage.xml -- test3.exe
程序会自动运行.bat文件,运行以后就可以生成测试报告了。(从bat文件可以看出,程序检测出test1,、test2、test3三个工程文件,并将工程文件分别进行qmake编译成exe执行文件,然后用openCppCoverage.exe生成测试报告)