From be7aac53247c7a87dc6a2dd6f86149c1495c7fbd Mon Sep 17 00:00:00 2001 From: Zhang Yong Date: Thu, 17 Jun 2021 16:44:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加单元测试框架 Log: 单元测试框架 Change-Id: Ib733bc56d8458f10a8930fe97a6f7310006c3fac --- CMakeLists.txt | 55 +++++++++++- tests/test-prj-running.sh | 58 ++++++++++++ tests/test_mainwindow.cpp | 40 +++++++++ tests/test_qtestmain.cpp | 182 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 333 insertions(+), 2 deletions(-) create mode 100755 tests/test-prj-running.sh create mode 100644 tests/test_mainwindow.cpp create mode 100644 tests/test_qtestmain.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 362123e..f6d437e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/tests/test-prj-running.sh b/tests/test-prj-running.sh new file mode 100755 index 0000000..5975f79 --- /dev/null +++ b/tests/test-prj-running.sh @@ -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 diff --git a/tests/test_mainwindow.cpp b/tests/test_mainwindow.cpp new file mode 100644 index 0000000..5e79234 --- /dev/null +++ b/tests/test_mainwindow.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd. + * + * Author: ZhangYong + * + * Maintainer: ZhangYong + * + * 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 . + */ +#include +#include + +#include +#include + +#define private public +#define protected public + +#include "ocrapplication.h" + +//初始拉起主界面 +TEST(MainWindow, mainwindow) +{ + OcrApplication instance; + instance.openFile(""); + QTest::qWait(2000); +} + +//其余case diff --git a/tests/test_qtestmain.cpp b/tests/test_qtestmain.cpp new file mode 100644 index 0000000..84c4481 --- /dev/null +++ b/tests/test_qtestmain.cpp @@ -0,0 +1,182 @@ +/* + * Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd. + * + * Author: ZhangYong + * + * Maintainer: ZhangYong + * + * 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 . + */ +#include +#include +#include + +// add necessary includes here +#include + +#include +#include + +#ifndef SYSTEM_MIPS +#include +#endif + +#include + +#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::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(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("string"); + QTest::addColumn("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("name"); + QTest::addColumn("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("event"); + QTest::addColumn("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"