feat: 增加单元测试框架

增加单元测试框架

Log: 单元测试框架
Change-Id: Ib733bc56d8458f10a8930fe97a6f7310006c3fac
This commit is contained in:
Zhang Yong 2021-06-17 16:44:04 +08:00
parent 805c18edb3
commit be7aac5324
4 changed files with 333 additions and 2 deletions

View File

@ -74,6 +74,58 @@ link_directories(${LINK_DIR})
add_executable(${PROJECT_NAME} ${allSource} ${AllQRC} ${LangSrcs} )
if(DOTEST)
SET(PROJECT_NAME_TEST
${PROJECT_NAME}_test)
include_directories(tests)
aux_source_directory(src/service allTestSource)
aux_source_directory(src/tessocrutils allTestSource)
aux_source_directory(src/view allTestSource)
aux_source_directory(tests allTestSource)
FILE(GLOB allTestSource1
"src/mainwidget.cpp"
"src/mainwindow.cpp"
"src/ocrapplication.cpp"
"src/resulttextview.cpp"
"src/textloadwidget.cpp")
add_executable(${PROJECT_NAME_TEST} ${allTestSource} ${allTestSource1})
target_link_libraries(${PROJECT_NAME_TEST} gmock gtest pthread)
add_test(${PROJECT_NAME_TEST} COMMAND ${PROJECT_NAME_TEST})
#------------------------------ 'make test'---------------------------------------
add_custom_target(test
COMMAND mkdir -p coverageResult
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_custom_command(TARGET test
COMMAND echo " =================== CREAT LCOV REPROT BEGIN ==================== "
COMMAND lcov --directory ./CMakeFiles/${PROJECT_NAME_TEST}.dir --zerocounters
COMMAND lcov --directory ./CMakeFiles/${PROJECT_NAME}.dir --zerocounters
COMMAND ./${PROJECT_NAME}/${PROJECT_NAME_TEST}
COMMAND lcov --directory . --capture --output-file ./coverageResult/${PROJECT_NAME}_Coverage.info
COMMAND echo " =================== do filter begin ==================== "
COMMAND lcov --remove ./coverageResult/${PROJECT_NAME}_Coverage.info
'*/${PROJECT_NAME_TEST}_autogen/*' '*/${PROJECT_NAME}_autogen/*' '*/usr/include/*' '*/tests/*' '*/googletest/*'
-o ./coverageResult/${PROJECT_NAME}_Coverage.info
COMMAND echo " =================== do filter end ==================== "
COMMAND genhtml -o ./coverageResult/report ./coverageResult/${PROJECT_NAME}_Coverage.info
COMMAND echo " =================== CREAT LCOV REPROT END ==================== "
# COMMAND nohup x-www-browser ${CMAKE_BINARY_DIR}/coverageResult/report/index.html &
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_dependencies(test ${PROJECT_NAME_TEST})
endif()
pkg_check_modules(3rd_lib REQUIRED
dtkwidget dtkgui
)
@ -109,8 +161,7 @@ install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION /usr/bin
INCLUDES DESTINATION allHeaders)
RUNTIME DESTINATION /usr/bin)
#install
install(FILES ${LangSrcs} DESTINATION ${TranslationDir})

58
tests/test-prj-running.sh Executable file
View File

@ -0,0 +1,58 @@
#!/bin/bash
#export QT_QPA_PLATFORM='offscreen'
export QTEST_FUNCTION_TIMEOUT='800000'
cd ..
rm -rf ./build-ut
rm -rf ./build
mkdir build-ut
cmake . -B build -D DOTEST=ON
cd build
#自动读取当前处理器核心数但考虑到服务器上会同时存在多个构建完全占用服务器CPU会导致构建变慢所以限制使用的核心不超过8个
JOBS=`cat /proc/cpuinfo| grep "processor"| wc -l`
if [ $JOBS -gt 8 ]
then JOBS=8
elif [ $JOBS -eq 0 ]
then JOBS=1
fi
echo use processor count: $JOBS
make -j$JOBS
lcov --directory ./CMakeFiles/deepin-ocr_test.dir --zerocounters
./deepin-ocr_test
lcov --directory . --capture --output-file ./coverageResult/deepin-ocr_Coverage.info
echo \ ===================\ do\ filter\ begin\ ====================\
lcov --remove ./coverageResult/deepin-ocr_Coverage.info '*/deepin-ocr_test_autogen/*' '*/deepin-ocr_autogen/*' '*/usr/include/*' '*/usr/local/*' '*/tests/*' '*/googletest/*' -o ./coverageResult/deepin-ocr_Coverage.info
echo \ ===================\ do\ filter\ end\ ====================\
genhtml -o ./coverageResult/report ./coverageResult/deepin-ocr_Coverage.info
sleep 2
lcov --directory . --capture --output-file ./coverageResult/deepin-ocr_Coverage.info
echo \ ===================\ do\ filter\ begin\ ====================\
lcov --remove ./coverageResult/deepin-ocr_Coverage.info '*/deepin-ocr_test_autogen/*' '*/deepin-ocr_autogen/*' '*/usr/include/*' '*/usr/local/*' '*/tests/*' '*/googletest/*' -o ./coverageResult/deepin-ocr_Coverage.info
echo \ ===================\ do\ filter\ end\ ====================\
genhtml -o ./coverageResult/report ./coverageResult/deepin-ocr_Coverage.info
cd ./../build-ut
cp -r ./../build/coverageResult/report/ ./
mv report html
cd html
mv index.html cov_deepin-ocr.html
cd ..
mkdir report
cd report
cp ./../../build/report/report_deepin-ocr.xml ./
cd ..
cp ./../build/asan_deepin-ocr.log* ./asan_deepin-ocr.log
exit 0

40
tests/test_mainwindow.cpp Normal file
View File

@ -0,0 +1,40 @@
/*
* Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd.
*
* Author: ZhangYong <zhangyong@uniontech.com>
*
* Maintainer: ZhangYong <ZhangYong@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
#include <QTestEventList>
#include <QObject>
#define private public
#define protected public
#include "ocrapplication.h"
//初始拉起主界面
TEST(MainWindow, mainwindow)
{
OcrApplication instance;
instance.openFile("");
QTest::qWait(2000);
}
//其余case

182
tests/test_qtestmain.cpp Normal file
View File

@ -0,0 +1,182 @@
/*
* Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd.
*
* Author: ZhangYong <zhangyong@uniontech.com>
*
* Maintainer: ZhangYong <ZhangYong@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtTest>
#include <QCoreApplication>
#include <QTest>
// add necessary includes here
#include <QLineEdit>
#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
#ifndef SYSTEM_MIPS
#include <sanitizer/asan_interface.h>
#endif
#include <DApplication>
#define private public
#define protected public
using namespace Dtk::Core;
using namespace Dtk::Widget;
#define QMYTEST_MAIN(TestObject) \
QT_BEGIN_NAMESPACE \
QTEST_ADD_GPU_BLACKLIST_SUPPORT_DEFS \
QT_END_NAMESPACE \
int main(int argc, char *argv[]) \
{ \
DApplication *dAppNew = new DApplication(argc, argv); \
dAppNew->setAttribute(Qt::AA_UseHighDpiPixmaps); \
dAppNew->setOrganizationName("deepin"); \
dAppNew->setApplicationName("deepin-ocr"); \
dAppNew->loadTranslator(QList<QLocale>() << QLocale::system()); \
QTEST_DISABLE_KEYPAD_NAVIGATION \
QTEST_ADD_GPU_BLACKLIST_SUPPORT \
TestObject tc; \
QTEST_SET_MAIN_SOURCE_PATH \
return QTest::qExec(&tc, argc, argv); \
} \
class QTestMain : public QObject
{
Q_OBJECT
public:
QTestMain();
~QTestMain();
private slots:
void initTestCase();
void cleanupTestCase();
void testGTest();
void testQString_data();
void testQString();
void testFloat_data();
void testFloat();
void testGui_data();
void testGui();
};
QTestMain::QTestMain()
{
}
QTestMain::~QTestMain()
{
}
void QTestMain::initTestCase()
{
}
void QTestMain::cleanupTestCase()
{
}
void QTestMain::testGTest()
{
testing::GTEST_FLAG(output) = "xml:./report/report_deepin-ocr.xml";
int argc = 1;
const auto arg0 = "dummy";
char *argv0 = const_cast<char *>(arg0);
testing::InitGoogleTest(&argc, &argv0);
int ret = RUN_ALL_TESTS();
Q_UNUSED(ret)
// 内存分析屏蔽mips
#ifndef SYSTEM_MIPS
__sanitizer_set_report_path("asan_deepin-ocr.log");
#endif
}
void QTestMain::testQString_data()
{
QTest::addColumn<QString>("string");
QTest::addColumn<QString>("result");
QTest::newRow("lower") << "hello" << "HELLO";
QTest::newRow("mix") << "heLLo" << "HELLO";
QTest::newRow("upper") << "HELLO" << "HELLO";
}
void QTestMain::testQString()
{
QFETCH(QString, string);
QFETCH(QString, result);
QCOMPARE(string.toUpper(), result);
QBENCHMARK{
Q_UNUSED(string.toUpper())
}
}
void QTestMain::testFloat_data()
{
QTest::addColumn<QString>("name");
QTest::addColumn<double>("score");
QTest::newRow("zhangsan") << "zhangsan" << 60.0;
QTest::newRow("lisi") << "lisi" << 56.0;
QTest::newRow("wanger") << "wanger" << 48.0;
}
void QTestMain::testFloat()
{
QFETCH(QString, name);
QFETCH(double, score);
QVERIFY2(score >= 30.0, name.toLocal8Bit() + " score: " + QString::number(score).toLocal8Bit());
}
void QTestMain::testGui_data()
{
QTest::addColumn<QTestEventList>("event");
QTest::addColumn<QString>("result");
QTestEventList list1;
list1.addKeyClicks("hello world");
QTest::newRow("item 0 ") << list1 << QString("hello world");
QTestEventList list2;
list2.addKeyClicks("abs0");
list2.addKeyClick(Qt::Key_Backspace);
QTest::newRow("item 1") << list2 << QString("abs");
}
void QTestMain::testGui()
{
// QFETCH(QTestEventList, event);
// QFETCH(QString, result);
// QLineEdit lineEdit;
// event.simulate(&lineEdit);
// QCOMPARE(lineEdit.text(), result);
}
QMYTEST_MAIN(QTestMain)
#include "test_qtestmain.moc"