diff --git a/AtlasTest/GoogleTestTools/CMakeLists.txt b/AtlasTest/GoogleTestTools/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..cd27426a77514b280456033601b068ae3f615f4b --- /dev/null +++ b/AtlasTest/GoogleTestTools/CMakeLists.txt @@ -0,0 +1,35 @@ +# $Id: CMakeLists.txt 744649 2016-05-03 19:33:39Z krasznaa $ +################################################################################ +# Package: GoogleTestTools +################################################################################ + +# Declare the package name: +atlas_subdir( GoogleTestTools ) + +# Declare the package's dependencies: +atlas_depends_on_subdirs( + PUBLIC + Control/AthenaCommon + TestPolicy + PRIVATE + GaudiKernel + AtlasGoogleTest ) + +# In standalone mode we just use the headers from the package. While in +# offline mode we build a proper library. +if( XAOD_STANDALONE ) + atlas_add_library( GoogleTestTools + GoogleTestTools/*.h + INTERFACE + PUBLIC_HEADERS GoogleTestTools ) +else() + atlas_add_library( GoogleTestTools + GoogleTestTools/*.h src/*.cxx + PUBLIC_HEADERS GoogleTestTools + PRIVATE_LINK_LIBRARIES GaudiKernel AtlasGoogleTest ) +endif() + +# Install files from the package: +# atlas_install_python_modules( python/*.py ) +# atlas_install_joboptions( share/*.py ) +# atlas_install_scripts( share/runUnitTests.sh share/post.sh ) diff --git a/AtlasTest/GoogleTestTools/GoogleTestTools/InitGaudiGoogleTest.h b/AtlasTest/GoogleTestTools/GoogleTestTools/InitGaudiGoogleTest.h new file mode 100644 index 0000000000000000000000000000000000000000..d4d3f097f58f9a5fa5eeadefdfae0255a016e08d --- /dev/null +++ b/AtlasTest/GoogleTestTools/GoogleTestTools/InitGaudiGoogleTest.h @@ -0,0 +1,57 @@ +// -*- mode: c++ -*- + +/** +@file TestTools/InitGaudiGoogleTest.h +@author S. Kluth +@date July 2016 +@brief Gaudi initialisation fixture base class for googletest based unit tests. +Test fixture classes for tests which need a fully configured Gaudi should inherit +from this class, and then provide further Gaudi configuration in their SetUp() +method if needed. +*/ + +#ifndef GOOGLETESTTOOLS_INITGAUDIGOOGLETEST_H +#define GOOGLETESTTOOLS_INITGAUDIGOOGLETEST_H + +#include "GaudiKernel/IMessageSvc.h" +#include "GaudiKernel/SmartIF.h" +#include "GaudiKernel/IProperty.h" +#include "GaudiKernel/ISvcLocator.h" +#include "GaudiKernel/ISvcManager.h" + +#include "gtest/gtest.h" + +class IAppMgrUI; + +namespace Athena_test { + + class InitGaudiGoogleTest : public ::testing::Test { + public: + + /** + @brief Ctor parameter: + @param level controls MessageSvc + */ + + InitGaudiGoogleTest( MSG::Level level=MSG::ALWAYS ); + + /** @brief dtor */ + + virtual ~InitGaudiGoogleTest(); + + /** @brief public members are visible in tests */ + + IAppMgrUI* theApp; + SmartIF<IProperty> propMgr; + SmartIF<ISvcLocator> svcLoc; + SmartIF<ISvcManager> svcMgr; + + private: + + MSG::Level m_msgLevel; + + }; + +} + +#endif diff --git a/AtlasTest/GoogleTestTools/cmt/requirements b/AtlasTest/GoogleTestTools/cmt/requirements new file mode 100644 index 0000000000000000000000000000000000000000..3ea01812fbd8e0c710f6af57ec8774e7a0adfed9 --- /dev/null +++ b/AtlasTest/GoogleTestTools/cmt/requirements @@ -0,0 +1,24 @@ +## automatically generated CMT requirements file +package GoogleTestTools +author sklutha + +## for athena policies: this has to be the first use statement +use AtlasPolicy AtlasPolicy-* + +## for gaudi tools, services and objects +use GaudiInterface GaudiInterface-* External + +## put here your package dependencies... + +branches src src/components doc python share GoogleTestTools + +library GoogleTestTools *.cxx +apply_pattern installed_library + +## default is to make component library. See: https://twiki.cern.ch/twiki/bin/view/Main/LearningAthena#Libraries_in_CMT for alternatives + +apply_pattern declare_joboptions files="*.py" +apply_pattern declare_python_modules files="*.py" + +use AtlasGoogleTest AtlasGoogleTest-* External + diff --git a/AtlasTest/GoogleTestTools/src/InitGaudiGoogleTest.cxx b/AtlasTest/GoogleTestTools/src/InitGaudiGoogleTest.cxx new file mode 100644 index 0000000000000000000000000000000000000000..eb92fb39eb3b46e7b97dd4c6b68cc1c5cd47efe9 --- /dev/null +++ b/AtlasTest/GoogleTestTools/src/InitGaudiGoogleTest.cxx @@ -0,0 +1,57 @@ + +#include "GoogleTestTools/InitGaudiGoogleTest.h" + +#include "GaudiKernel/Bootstrap.h" +#include "GaudiKernel/IAppMgrUI.h" + +#include <iostream> +#include <fstream> + +namespace Athena_test { + + InitGaudiGoogleTest::InitGaudiGoogleTest( MSG::Level level ) : + // create a new ApplicationMgr w/o singleton behaviour + theApp( Gaudi::createApplicationMgrEx( "GaudiCoreSvc", + "ApplicationMgr" ) ), + propMgr( theApp ), + svcLoc( theApp ), + svcMgr( theApp ), + m_msgLevel( level ) { + EXPECT_TRUE( theApp != nullptr ); + EXPECT_TRUE( propMgr.isValid() ); + EXPECT_TRUE( svcLoc.isValid() ); + EXPECT_TRUE( svcMgr.isValid() ); + // set the new ApplicationMgr as instance in Gaudi + Gaudi::setInstance( theApp ); + EXPECT_TRUE( propMgr->setProperty( "JobOptionsType", "NONE" ).isSuccess() ); + EXPECT_TRUE( propMgr->setProperty( "OutputLevel", + std::to_string( m_msgLevel ) ).isSuccess() ); + bool lconfig= false; + // Configure ApplicationMgr + if( m_msgLevel == MSG::ALWAYS ) { + // Redirect cout to /dev/null around configure() to get rid of Gaudi + // messages in unit tests + std::streambuf* coutbuf= std::cout.rdbuf(); // save std::cout buf + std::ofstream out( "/dev/null" ); + std::cout.rdbuf( out.rdbuf() ); // redirect std::cout + lconfig= theApp->configure().isSuccess(); + std::cout.rdbuf( coutbuf ); // restore std::cout + } + else { + // just configure() + lconfig= theApp->configure().isSuccess(); + } + EXPECT_TRUE( lconfig ) << "InitGaudiGoogleTest: theApp->configure() failure"; + EXPECT_TRUE( theApp->initialize().isSuccess() ); + } + + // Finalize and terminate Gaudi, more to make sure that things + // went clean than anything else because we are throwing away the + // ApplicationMgr anyway + InitGaudiGoogleTest::~InitGaudiGoogleTest() { + EXPECT_TRUE( theApp->finalize().isSuccess() ); + EXPECT_TRUE( theApp->terminate().isSuccess() ); + } + +} +