From 22ccb366e847ce61d7d1e0e136957312f07b173d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jakub=20Klinkovsk=C3=BD?= <klinkjak@fjfi.cvut.cz>
Date: Fri, 12 Apr 2019 14:06:54 +0200
Subject: [PATCH] Tests: split main functions into separate headers

---
 src/UnitTests/AssertCudaTest.cu               | 11 +--
 src/UnitTests/AssertTest.cpp                  | 11 +--
 .../Containers/ArrayOperationsTest.h          | 11 +--
 src/UnitTests/Containers/ArrayTest.h          | 11 +--
 src/UnitTests/Containers/ArrayViewTest.h      | 11 +--
 .../Containers/DistributedArrayTest.h         | 62 +---------------
 .../Containers/DistributedVectorTest.h        | 62 +---------------
 src/UnitTests/Containers/ListTest.cpp         | 11 +--
 .../Containers/Multimaps/MultimapTest.cpp     | 12 +---
 .../Multimaps/StaticMultimapTest.cpp          | 12 +---
 src/UnitTests/Containers/MultireductionTest.h | 11 +--
 src/UnitTests/Containers/StaticArrayTest.cpp  | 11 +--
 src/UnitTests/Containers/StaticVectorTest.cpp | 11 +--
 src/UnitTests/Containers/VectorTest.h         | 11 +--
 src/UnitTests/FileNameTest.cpp                | 13 +---
 src/UnitTests/FileTest.h                      | 11 +--
 .../Functions/BoundaryMeshFunctionTest.h      | 15 +---
 src/UnitTests/Functions/MeshFunctionTest.h    | 16 +----
 src/UnitTests/Matrices/DenseMatrixTest.h      | 12 +---
 .../Matrices/DistributedMatrixTest.h          | 62 +---------------
 src/UnitTests/Matrices/SparseMatrixCopyTest.h | 11 +--
 src/UnitTests/Matrices/SparseMatrixTest.h     | 11 +--
 .../Matrices/SparseMatrixTest_AdEllpack.h     | 11 +--
 .../Matrices/SparseMatrixTest_BiEllpack.h     | 11 +--
 src/UnitTests/Matrices/SparseMatrixTest_CSR.h | 11 +--
 .../SparseMatrixTest_ChunkedEllpack.h         | 11 +--
 .../Matrices/SparseMatrixTest_Ellpack.h       | 11 +--
 .../Matrices/SparseMatrixTest_SlicedEllpack.h | 11 +--
 src/UnitTests/Meshes/BoundaryTagsTest.cpp     | 11 +--
 .../DistributedMeshes/CopyEntitiesTest.cpp    | 12 +---
 .../CutDistributedGridTest.cpp                | 69 +-----------------
 .../CutDistributedMeshFunctionTest.cpp        | 69 +-----------------
 .../DistributedMeshes/CutMeshFunctionTest.cpp | 11 +--
 .../DistributedMeshes/DirectionsTest.cpp      | 13 +---
 .../DistributedGridIOTestBase.h               | 67 +----------------
 .../DistributedGridIO_MPIIOTestBase.h         | 66 +----------------
 .../DistributedGridTest_1D.cpp                | 66 +----------------
 .../DistributedGridTest_2D.cpp                | 68 +-----------------
 .../DistributedGridTest_3D.cpp                | 70 +-----------------
 .../DistributedVectorFieldIO_MPIIOTest.cpp    | 71 +------------------
 src/UnitTests/Meshes/MeshEntityTest.cpp       | 11 +--
 src/UnitTests/Meshes/MeshOrderingTest.cpp     | 11 +--
 src/UnitTests/Meshes/MeshOrderingTest.cu      | 11 +--
 src/UnitTests/Meshes/MeshTest.cpp             | 11 +--
 src/UnitTests/Meshes/MeshTest.cu              | 11 +--
 src/UnitTests/ObjectTest.cpp                  | 14 +---
 .../Pointers/SharedPointerCudaTest.cu         | 39 ++++------
 .../Pointers/SharedPointerHostTest.cpp        | 24 ++-----
 src/UnitTests/Pointers/UniquePointerTest.cpp  | 15 +---
 src/UnitTests/SaveAndLoadMeshfunctionTest.cpp | 14 +---
 src/UnitTests/StringTest.cpp                  | 13 +---
 src/UnitTests/TimerTest.cpp                   | 18 +----
 src/UnitTests/main.h                          | 15 ++++
 src/UnitTests/main_mpi.h                      | 66 +++++++++++++++++
 54 files changed, 162 insertions(+), 1179 deletions(-)
 create mode 100644 src/UnitTests/main.h
 create mode 100644 src/UnitTests/main_mpi.h

diff --git a/src/UnitTests/AssertCudaTest.cu b/src/UnitTests/AssertCudaTest.cu
index cfd4e846b7..9d4865eb9c 100644
--- a/src/UnitTests/AssertCudaTest.cu
+++ b/src/UnitTests/AssertCudaTest.cu
@@ -108,13 +108,4 @@ WRAP_ASSERT( test28, TNL_ASSERT_FALSE( data_full, "non-nullptr is false" );, fal
 #endif
 #endif
 
-#include "GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "main.h"
diff --git a/src/UnitTests/AssertTest.cpp b/src/UnitTests/AssertTest.cpp
index ea20f7957a..88bbcf4bee 100644
--- a/src/UnitTests/AssertTest.cpp
+++ b/src/UnitTests/AssertTest.cpp
@@ -68,13 +68,4 @@ TEST( AssertTest, basicTest )
 }
 #endif
 
-#include "GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "main.h"
diff --git a/src/UnitTests/Containers/ArrayOperationsTest.h b/src/UnitTests/Containers/ArrayOperationsTest.h
index 23b8fcd4eb..4c1b6b9c2c 100644
--- a/src/UnitTests/Containers/ArrayOperationsTest.h
+++ b/src/UnitTests/Containers/ArrayOperationsTest.h
@@ -442,13 +442,4 @@ TYPED_TEST( ArrayOperationsTest, containsOnlyValue_cuda )
 #endif // HAVE_GTEST
 
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Containers/ArrayTest.h b/src/UnitTests/Containers/ArrayTest.h
index 62bf493cbc..dc054e1a86 100644
--- a/src/UnitTests/Containers/ArrayTest.h
+++ b/src/UnitTests/Containers/ArrayTest.h
@@ -525,13 +525,4 @@ TYPED_TEST( ArrayTest, boundLoad )
 #endif // HAVE_GTEST
 
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Containers/ArrayViewTest.h b/src/UnitTests/Containers/ArrayViewTest.h
index 28d8b512b1..914d5a5812 100644
--- a/src/UnitTests/Containers/ArrayViewTest.h
+++ b/src/UnitTests/Containers/ArrayViewTest.h
@@ -545,13 +545,4 @@ TYPED_TEST( ArrayViewTest, assignmentOperatorWithDifferentType )
 #endif // HAVE_GTEST
 
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Containers/DistributedArrayTest.h b/src/UnitTests/Containers/DistributedArrayTest.h
index 381539af6b..03d94ccbe3 100644
--- a/src/UnitTests/Containers/DistributedArrayTest.h
+++ b/src/UnitTests/Containers/DistributedArrayTest.h
@@ -11,7 +11,6 @@
 
 #include <TNL/Communicators/MpiCommunicator.h>
 #include <TNL/Communicators/NoDistrCommunicator.h>
-#include <TNL/Communicators/ScopedInitializer.h>
 #include <TNL/Containers/DistributedArray.h>
 #include <TNL/Containers/Partitioner.h>
 
@@ -288,63 +287,4 @@ TYPED_TEST( DistributedArrayTest, boolOperator )
 
 #endif  // HAVE_GTEST
 
-
-#if (defined(HAVE_GTEST) && defined(HAVE_MPI))
-using CommunicatorType = Communicators::MpiCommunicator;
-
-#include <sstream>
-
-class MinimalistBufferedPrinter
-: public ::testing::EmptyTestEventListener
-{
-private:
-   std::stringstream sout;
-
-public:
-   // Called before a test starts.
-   virtual void OnTestStart(const ::testing::TestInfo& test_info)
-   {
-      sout << test_info.test_case_name() << "." << test_info.name() << " Start." << std::endl;
-   }
-
-   // Called after a failed assertion or a SUCCEED() invocation.
-   virtual void OnTestPartResult(const ::testing::TestPartResult& test_part_result)
-   {
-      sout << (test_part_result.failed() ? "====Failure=== " : "===Success=== ")
-           << test_part_result.file_name() << " "
-           << test_part_result.line_number() <<std::endl
-           << test_part_result.summary() <<std::endl;
-   }
-
-   // Called after a test ends.
-   virtual void OnTestEnd(const ::testing::TestInfo& test_info)
-   {
-      const int rank = CommunicatorType::GetRank(CommunicatorType::AllGroup);
-      sout << test_info.test_case_name() << "." << test_info.name() << " End." <<std::endl;
-      std::cout << rank << ":" << std::endl << sout.str()<< std::endl;
-      sout.str( std::string() );
-      sout.clear();
-   }
-};
-#endif
-
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-
-   #ifdef HAVE_MPI
-      ::testing::TestEventListeners& listeners =
-         ::testing::UnitTest::GetInstance()->listeners();
-
-      delete listeners.Release(listeners.default_result_printer());
-      listeners.Append(new MinimalistBufferedPrinter);
-
-      Communicators::ScopedInitializer< CommunicatorType > mpi(argc, argv);
-   #endif
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main_mpi.h"
diff --git a/src/UnitTests/Containers/DistributedVectorTest.h b/src/UnitTests/Containers/DistributedVectorTest.h
index 870446d15a..3b52055f44 100644
--- a/src/UnitTests/Containers/DistributedVectorTest.h
+++ b/src/UnitTests/Containers/DistributedVectorTest.h
@@ -44,7 +44,6 @@ void setNegativeLinearSequence( Vector& deviceVector )
 
 #include <TNL/Communicators/MpiCommunicator.h>
 #include <TNL/Communicators/NoDistrCommunicator.h>
-#include <TNL/Communicators/ScopedInitializer.h>
 #include <TNL/Containers/DistributedVector.h>
 #include <TNL/Containers/DistributedVectorView.h>
 #include <TNL/Containers/Partitioner.h>
@@ -349,63 +348,4 @@ TYPED_TEST( DistributedVectorTest, addVectors )
 
 #endif  // HAVE_GTEST
 
-
-#if (defined(HAVE_GTEST) && defined(HAVE_MPI))
-using CommunicatorType = Communicators::MpiCommunicator;
-
-#include <sstream>
-
-class MinimalistBufferedPrinter
-: public ::testing::EmptyTestEventListener
-{
-private:
-   std::stringstream sout;
-
-public:
-   // Called before a test starts.
-   virtual void OnTestStart(const ::testing::TestInfo& test_info)
-   {
-      sout << test_info.test_case_name() << "." << test_info.name() << " Start." << std::endl;
-   }
-
-   // Called after a failed assertion or a SUCCEED() invocation.
-   virtual void OnTestPartResult(const ::testing::TestPartResult& test_part_result)
-   {
-      sout << (test_part_result.failed() ? "====Failure=== " : "===Success=== ")
-           << test_part_result.file_name() << " "
-           << test_part_result.line_number() <<std::endl
-           << test_part_result.summary() <<std::endl;
-   }
-
-   // Called after a test ends.
-   virtual void OnTestEnd(const ::testing::TestInfo& test_info)
-   {
-      int rank=CommunicatorType::GetRank(CommunicatorType::AllGroup);
-      sout << test_info.test_case_name() << "." << test_info.name() << " End." <<std::endl;
-      std::cout << rank << ":" << std::endl << sout.str()<< std::endl;
-      sout.str( std::string() );
-      sout.clear();
-   }
-};
-#endif
-
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-
-   #ifdef HAVE_MPI
-      ::testing::TestEventListeners& listeners =
-         ::testing::UnitTest::GetInstance()->listeners();
-
-      delete listeners.Release(listeners.default_result_printer());
-      listeners.Append(new MinimalistBufferedPrinter);
-
-      Communicators::ScopedInitializer< CommunicatorType > mpi(argc, argv);
-   #endif
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main_mpi.h"
diff --git a/src/UnitTests/Containers/ListTest.cpp b/src/UnitTests/Containers/ListTest.cpp
index 32e65c2231..8330e5268a 100644
--- a/src/UnitTests/Containers/ListTest.cpp
+++ b/src/UnitTests/Containers/ListTest.cpp
@@ -143,13 +143,4 @@ TYPED_TEST( ListTest, operations )
 #endif
 
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Containers/Multimaps/MultimapTest.cpp b/src/UnitTests/Containers/Multimaps/MultimapTest.cpp
index 4612c1dd37..1ba445e2c6 100644
--- a/src/UnitTests/Containers/Multimaps/MultimapTest.cpp
+++ b/src/UnitTests/Containers/Multimaps/MultimapTest.cpp
@@ -7,7 +7,7 @@ using IndexType = int;
 using Device = Devices::Host;
 using LocalIndexType = short;
 
-#ifdef HAVE_GTEST 
+#ifdef HAVE_GTEST
 #include <gtest/gtest.h>
 
 TEST( MultimapTest, TestTypedefs )
@@ -147,12 +147,4 @@ TEST( MultimapTest, TestSaveAndLoad )
 }
 #endif
 
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   return EXIT_FAILURE;
-#endif
-}
+#include "../../main.h"
diff --git a/src/UnitTests/Containers/Multimaps/StaticMultimapTest.cpp b/src/UnitTests/Containers/Multimaps/StaticMultimapTest.cpp
index 3602f53ca8..d4ad0bcf73 100644
--- a/src/UnitTests/Containers/Multimaps/StaticMultimapTest.cpp
+++ b/src/UnitTests/Containers/Multimaps/StaticMultimapTest.cpp
@@ -7,7 +7,7 @@ using IndexType = int;
 using Device = Devices::Host;
 using LocalIndexType = short;
 
-#ifdef HAVE_GTEST 
+#ifdef HAVE_GTEST
 #include <gtest/gtest.h>
 
 TEST( MultimapTest, TestTypedefs )
@@ -95,12 +95,4 @@ TEST( MultimapTest, TestSaveAndLoad )
 }
 #endif
 
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   return EXIT_FAILURE;
-#endif
-}
+#include "../../main.h"
diff --git a/src/UnitTests/Containers/MultireductionTest.h b/src/UnitTests/Containers/MultireductionTest.h
index d0aa7bd84d..0487e916b8 100644
--- a/src/UnitTests/Containers/MultireductionTest.h
+++ b/src/UnitTests/Containers/MultireductionTest.h
@@ -120,13 +120,4 @@ TYPED_TEST( MultireductionTest, scalarProduct )
 #endif // HAVE_GTEST
 
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Containers/StaticArrayTest.cpp b/src/UnitTests/Containers/StaticArrayTest.cpp
index 1297933e76..f1f6b208d2 100644
--- a/src/UnitTests/Containers/StaticArrayTest.cpp
+++ b/src/UnitTests/Containers/StaticArrayTest.cpp
@@ -283,13 +283,4 @@ TYPED_TEST( StaticArrayTest, streamOperator )
 #endif // HAVE_GTEST
 
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Containers/StaticVectorTest.cpp b/src/UnitTests/Containers/StaticVectorTest.cpp
index e34db3f338..3f39c50452 100644
--- a/src/UnitTests/Containers/StaticVectorTest.cpp
+++ b/src/UnitTests/Containers/StaticVectorTest.cpp
@@ -191,13 +191,4 @@ TYPED_TEST( StaticVectorTest, lpNorm )
 #endif
 
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Containers/VectorTest.h b/src/UnitTests/Containers/VectorTest.h
index 5f84245165..c223a7afd0 100644
--- a/src/UnitTests/Containers/VectorTest.h
+++ b/src/UnitTests/Containers/VectorTest.h
@@ -791,13 +791,4 @@ TEST( VectorSpecialCasesTest, defaultConstructors )
 #endif // HAVE_GTEST
 
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/FileNameTest.cpp b/src/UnitTests/FileNameTest.cpp
index 5a69a1f288..b731db6a08 100644
--- a/src/UnitTests/FileNameTest.cpp
+++ b/src/UnitTests/FileNameTest.cpp
@@ -75,15 +75,4 @@ TEST( FileNameTest, AllTogether )
 }
 #endif
 
-
-#include "GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
-
+#include "main.h"
diff --git a/src/UnitTests/FileTest.h b/src/UnitTests/FileTest.h
index 4a69fcb202..1e0db34f4a 100644
--- a/src/UnitTests/FileTest.h
+++ b/src/UnitTests/FileTest.h
@@ -205,13 +205,4 @@ TEST( FileTest, WriteAndReadCUDAWithConversion )
 #endif
 #endif
 
-#include "GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "main.h"
diff --git a/src/UnitTests/Functions/BoundaryMeshFunctionTest.h b/src/UnitTests/Functions/BoundaryMeshFunctionTest.h
index bab8bf5afb..d3e0a48f55 100644
--- a/src/UnitTests/Functions/BoundaryMeshFunctionTest.h
+++ b/src/UnitTests/Functions/BoundaryMeshFunctionTest.h
@@ -10,9 +10,7 @@
 
 #pragma once
 
-#include "../GtestMissingError.h"
-
-#ifdef HAVE_GTEST 
+#ifdef HAVE_GTEST
 #include <gtest/gtest.h>
 #include <TNL/Functions/BoundaryMeshFunction.h>
 #include <TNL/Meshes/Grid.h>
@@ -22,16 +20,7 @@ TEST( BoundaryMeshFunctionTest, BasicConstructor )
    using Grid = TNL::Meshes::Grid< 2 >;
    TNL::Functions::BoundaryMeshFunction< Grid > boundaryMesh;
 }
-
 #endif
 
 
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Functions/MeshFunctionTest.h b/src/UnitTests/Functions/MeshFunctionTest.h
index b60daf091a..8c8b77c090 100644
--- a/src/UnitTests/Functions/MeshFunctionTest.h
+++ b/src/UnitTests/Functions/MeshFunctionTest.h
@@ -10,9 +10,7 @@
 
 #pragma once
 
-#include "../GtestMissingError.h"
-
-#ifdef HAVE_GTEST 
+#ifdef HAVE_GTEST
 #include <gtest/gtest.h>
 #include <sstream>
 #include <TNL/Functions/MeshFunction.h>
@@ -35,7 +33,7 @@ TEST( MeshFunctionTest, OstreamOperatorTest )
    grid->setDimensions( CoordinatesType( 3, 3 ) );
    MeshFunctionType meshFunction( grid );
    meshFunction.getData().setValue( 1.0 );
-   
+
    const char* str = "[ 1, 1, 1, 1, 1, 1, 1, 1, 1 ]";
    std::stringstream string_stream1, string_stream2( str );
    string_stream1 << meshFunction;
@@ -46,12 +44,4 @@ TEST( MeshFunctionTest, OstreamOperatorTest )
 #endif
 
 
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Matrices/DenseMatrixTest.h b/src/UnitTests/Matrices/DenseMatrixTest.h
index ea4b6174dc..f1661c997e 100644
--- a/src/UnitTests/Matrices/DenseMatrixTest.h
+++ b/src/UnitTests/Matrices/DenseMatrixTest.h
@@ -1651,14 +1651,4 @@ TEST( DenseMatrixTest, Dense_performSORIterationTest_Cuda )
 
 #endif
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
-
+#include "../main.h"
diff --git a/src/UnitTests/Matrices/DistributedMatrixTest.h b/src/UnitTests/Matrices/DistributedMatrixTest.h
index 2400b7a9ed..a2170dd1e9 100644
--- a/src/UnitTests/Matrices/DistributedMatrixTest.h
+++ b/src/UnitTests/Matrices/DistributedMatrixTest.h
@@ -41,7 +41,6 @@ void setMatrix( Matrix& matrix, const RowLengths& rowLengths )
 
 #include <TNL/Communicators/MpiCommunicator.h>
 #include <TNL/Communicators/NoDistrCommunicator.h>
-#include <TNL/Communicators/ScopedInitializer.h>
 #include <TNL/Matrices/DistributedMatrix.h>
 #include <TNL/Containers/Partitioner.h>
 #include <TNL/Matrices/CSR.h>
@@ -237,63 +236,4 @@ TYPED_TEST( DistributedMatrixTest, vectorProduct_distributedInput )
 
 #endif  // HAVE_GTEST
 
-
-#if (defined(HAVE_GTEST) && defined(HAVE_MPI))
-using CommunicatorType = Communicators::MpiCommunicator;
-
-#include <sstream>
-
-class MinimalistBufferedPrinter
-: public ::testing::EmptyTestEventListener
-{
-private:
-   std::stringstream sout;
-
-public:
-   // Called before a test starts.
-   virtual void OnTestStart(const ::testing::TestInfo& test_info)
-   {
-      sout << test_info.test_case_name() << "." << test_info.name() << " Start." << std::endl;
-   }
-
-   // Called after a failed assertion or a SUCCEED() invocation.
-   virtual void OnTestPartResult(const ::testing::TestPartResult& test_part_result)
-   {
-      sout << (test_part_result.failed() ? "====Failure=== " : "===Success=== ")
-           << test_part_result.file_name() << " "
-           << test_part_result.line_number() <<std::endl
-           << test_part_result.summary() <<std::endl;
-   }
-
-   // Called after a test ends.
-   virtual void OnTestEnd(const ::testing::TestInfo& test_info)
-   {
-      int rank=CommunicatorType::GetRank(CommunicatorType::AllGroup);
-      sout << test_info.test_case_name() << "." << test_info.name() << " End." <<std::endl;
-      std::cout << rank << ":" << std::endl << sout.str()<< std::endl;
-      sout.str( std::string() );
-      sout.clear();
-   }
-};
-#endif
-
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-
-   #ifdef HAVE_MPI
-      ::testing::TestEventListeners& listeners =
-         ::testing::UnitTest::GetInstance()->listeners();
-
-      delete listeners.Release(listeners.default_result_printer());
-      listeners.Append(new MinimalistBufferedPrinter);
-
-      Communicators::ScopedInitializer< CommunicatorType > mpi(argc, argv);
-   #endif
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main_mpi.h"
diff --git a/src/UnitTests/Matrices/SparseMatrixCopyTest.h b/src/UnitTests/Matrices/SparseMatrixCopyTest.h
index 2885bac093..a38f294979 100644
--- a/src/UnitTests/Matrices/SparseMatrixCopyTest.h
+++ b/src/UnitTests/Matrices/SparseMatrixCopyTest.h
@@ -551,13 +551,4 @@ TEST( SparseMatrixCopyTest, SlicedEllpack_to_Ellpack_cuda )
 
 #endif
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Matrices/SparseMatrixTest.h b/src/UnitTests/Matrices/SparseMatrixTest.h
index 78121bdf2a..c3716c116f 100644
--- a/src/UnitTests/Matrices/SparseMatrixTest.h
+++ b/src/UnitTests/Matrices/SparseMatrixTest.h
@@ -50,13 +50,4 @@ TEST( SparseMatrixTest, CSR_perforSORIterationTest_Cuda )
 
 #endif
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Matrices/SparseMatrixTest_AdEllpack.h b/src/UnitTests/Matrices/SparseMatrixTest_AdEllpack.h
index 24b4db1b2d..f6ab325b03 100644
--- a/src/UnitTests/Matrices/SparseMatrixTest_AdEllpack.h
+++ b/src/UnitTests/Matrices/SparseMatrixTest_AdEllpack.h
@@ -139,13 +139,4 @@ TYPED_TEST( AdEllpackMatrixTest, printTest )
 #endif
 
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Matrices/SparseMatrixTest_BiEllpack.h b/src/UnitTests/Matrices/SparseMatrixTest_BiEllpack.h
index a18e7cc52c..c3dd6abb4e 100644
--- a/src/UnitTests/Matrices/SparseMatrixTest_BiEllpack.h
+++ b/src/UnitTests/Matrices/SparseMatrixTest_BiEllpack.h
@@ -138,13 +138,4 @@ TYPED_TEST( BiEllpackMatrixTest, printTest )
 
 #endif
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Matrices/SparseMatrixTest_CSR.h b/src/UnitTests/Matrices/SparseMatrixTest_CSR.h
index 2eaecc76e7..9707f6b841 100644
--- a/src/UnitTests/Matrices/SparseMatrixTest_CSR.h
+++ b/src/UnitTests/Matrices/SparseMatrixTest_CSR.h
@@ -136,13 +136,4 @@ TYPED_TEST( CSRMatrixTest, printTest )
 
 #endif
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Matrices/SparseMatrixTest_ChunkedEllpack.h b/src/UnitTests/Matrices/SparseMatrixTest_ChunkedEllpack.h
index 99ad9f0b45..1845255d3a 100644
--- a/src/UnitTests/Matrices/SparseMatrixTest_ChunkedEllpack.h
+++ b/src/UnitTests/Matrices/SparseMatrixTest_ChunkedEllpack.h
@@ -141,13 +141,4 @@ TYPED_TEST( ChunkedEllpackMatrixTest, printTest )
 
 #endif
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Matrices/SparseMatrixTest_Ellpack.h b/src/UnitTests/Matrices/SparseMatrixTest_Ellpack.h
index c6ecd72b38..f20208b7c5 100644
--- a/src/UnitTests/Matrices/SparseMatrixTest_Ellpack.h
+++ b/src/UnitTests/Matrices/SparseMatrixTest_Ellpack.h
@@ -136,13 +136,4 @@ TYPED_TEST( EllpackMatrixTest, printTest )
 
 #endif
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Matrices/SparseMatrixTest_SlicedEllpack.h b/src/UnitTests/Matrices/SparseMatrixTest_SlicedEllpack.h
index 0fc1f59e2f..5341847711 100644
--- a/src/UnitTests/Matrices/SparseMatrixTest_SlicedEllpack.h
+++ b/src/UnitTests/Matrices/SparseMatrixTest_SlicedEllpack.h
@@ -136,13 +136,4 @@ TYPED_TEST( SlicedEllpackMatrixTest, printTest )
 
 #endif
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Meshes/BoundaryTagsTest.cpp b/src/UnitTests/Meshes/BoundaryTagsTest.cpp
index 274ef974b7..c83e407526 100644
--- a/src/UnitTests/Meshes/BoundaryTagsTest.cpp
+++ b/src/UnitTests/Meshes/BoundaryTagsTest.cpp
@@ -1,11 +1,2 @@
 #include "BoundaryTagsTest.h"
-
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   return EXIT_FAILURE;
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Meshes/DistributedMeshes/CopyEntitiesTest.cpp b/src/UnitTests/Meshes/DistributedMeshes/CopyEntitiesTest.cpp
index 383f8328b3..1f6ddf2c7d 100644
--- a/src/UnitTests/Meshes/DistributedMeshes/CopyEntitiesTest.cpp
+++ b/src/UnitTests/Meshes/DistributedMeshes/CopyEntitiesTest.cpp
@@ -224,14 +224,4 @@ TEST( CopyEntitiesTest, 3D )
 
 #endif
 
-#include "../../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
-
+#include "../../main.h"
diff --git a/src/UnitTests/Meshes/DistributedMeshes/CutDistributedGridTest.cpp b/src/UnitTests/Meshes/DistributedMeshes/CutDistributedGridTest.cpp
index 587ec807ec..54071032c5 100644
--- a/src/UnitTests/Meshes/DistributedMeshes/CutDistributedGridTest.cpp
+++ b/src/UnitTests/Meshes/DistributedMeshes/CutDistributedGridTest.cpp
@@ -4,7 +4,6 @@
 #ifdef HAVE_MPI  
 
 #include <TNL/Communicators/MpiCommunicator.h>
-#include <TNL/Communicators/ScopedInitializer.h>
 #include <TNL/Meshes/DistributedMeshes/DistributedMesh.h>
 #include <TNL/Meshes/DistributedMeshes/SubdomainOverlapsGetter.h>
 
@@ -356,74 +355,8 @@ TEST(CutDistributedGirdTest_3D, IsNotDistributed_2D)
         EXPECT_FALSE(cutDistributedGrid.isDistributed()) << "Řez by neměl být distribuovaný";
     }
 }
-
-
-
-#else
-TEST(NoMPI, NoTest)
-{
-    ASSERT_TRUE(true) << ":-(";
-}
 #endif
 
 #endif
 
-
-#if (defined(HAVE_GTEST) && defined(HAVE_MPI))
-#include <sstream>
-
-  class MinimalistBufferedPrinter : public ::testing::EmptyTestEventListener {
-      
-  private:
-      std::stringstream sout;
-      
-  public:
-      
-    // Called before a test starts.
-    virtual void OnTestStart(const ::testing::TestInfo& test_info) {
-      sout<< test_info.test_case_name() <<"." << test_info.name() << " Start." <<std::endl;
-    }
-
-    // Called after a failed assertion or a SUCCEED() invocation.
-    virtual void OnTestPartResult(
-        const ::testing::TestPartResult& test_part_result) {
-      sout << (test_part_result.failed() ? "====Failure=== " : "===Success=== ") 
-              << test_part_result.file_name() << " "
-              << test_part_result.line_number() <<std::endl
-              << test_part_result.summary() <<std::endl;
-    }
-
-    // Called after a test ends.
-    virtual void OnTestEnd(const ::testing::TestInfo& test_info) 
-    {
-        int rank=CommunicatorType::GetRank(CommunicatorType::AllGroup);
-        sout<< test_info.test_case_name() <<"." << test_info.name() << " End." <<std::endl;
-        std::cout << rank << ":" << std::endl << sout.str()<< std::endl;
-        sout.str( std::string() );
-        sout.clear();
-    }
-  };
-#endif
-
-#include "../../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-
-    #ifdef HAVE_MPI
-       ::testing::TestEventListeners& listeners =
-          ::testing::UnitTest::GetInstance()->listeners();
-
-       delete listeners.Release(listeners.default_result_printer());
-       listeners.Append(new MinimalistBufferedPrinter);
-
-       Communicators::ScopedInitializer< CommunicatorType > mpi(argc, argv);
-    #endif
-       return RUN_ALL_TESTS();
-#else
-   
-   throw GtestMissingError();
-#endif
-}
-
+#include "../../main_mpi.h"
diff --git a/src/UnitTests/Meshes/DistributedMeshes/CutDistributedMeshFunctionTest.cpp b/src/UnitTests/Meshes/DistributedMeshes/CutDistributedMeshFunctionTest.cpp
index caaf3f613e..7a19611dae 100644
--- a/src/UnitTests/Meshes/DistributedMeshes/CutDistributedMeshFunctionTest.cpp
+++ b/src/UnitTests/Meshes/DistributedMeshes/CutDistributedMeshFunctionTest.cpp
@@ -6,7 +6,6 @@
 #include <TNL/Devices/Host.h> 
 #include <TNL/Functions/CutMeshFunction.h>
 #include <TNL/Communicators/MpiCommunicator.h>
-#include <TNL/Communicators/ScopedInitializer.h>
 #include <TNL/Meshes/DistributedMeshes/DistributedGridIO.h>
 #include <TNL/Meshes/DistributedMeshes/SubdomainOverlapsGetter.h>
 
@@ -639,74 +638,8 @@ TEST(CutDistributedMeshFunction, 3D_2_Save)
       }
   
 }
-
-
-
-#else
-TEST(NoMPI, NoTest)
-{
-    ASSERT_TRUE(true) << ":-(";
-}
-#endif
-
-#endif
-
-
-#if (defined(HAVE_GTEST) && defined(HAVE_MPI))
-#include <sstream>
-
-  class MinimalistBufferedPrinter : public ::testing::EmptyTestEventListener {
-      
-  private:
-      std::stringstream sout;
-      
-  public:
-      
-    // Called before a test starts.
-    virtual void OnTestStart(const ::testing::TestInfo& test_info) {
-      sout<< test_info.test_case_name() <<"." << test_info.name() << " Start." <<std::endl;
-    }
-
-    // Called after a failed assertion or a SUCCEED() invocation.
-    virtual void OnTestPartResult(
-        const ::testing::TestPartResult& test_part_result) {
-      sout << (test_part_result.failed() ? "====Failure=== " : "===Success=== ") 
-              << test_part_result.file_name() << " "
-              << test_part_result.line_number() <<std::endl
-              << test_part_result.summary() <<std::endl;
-    }
-
-    // Called after a test ends.
-    virtual void OnTestEnd(const ::testing::TestInfo& test_info) 
-    {
-        int rank=CommunicatorType::GetRank(CommunicatorType::AllGroup);
-        sout<< test_info.test_case_name() <<"." << test_info.name() << " End." <<std::endl;
-        std::cout << rank << ":" << std::endl << sout.str()<< std::endl;
-        sout.str( std::string() );
-        sout.clear();
-    }
-  };
 #endif
 
-#include "../../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-
-    #ifdef HAVE_MPI
-       ::testing::TestEventListeners& listeners =
-          ::testing::UnitTest::GetInstance()->listeners();
-
-       delete listeners.Release(listeners.default_result_printer());
-       listeners.Append(new MinimalistBufferedPrinter);
-
-       Communicators::ScopedInitializer< CommunicatorType > mpi(argc, argv);
-    #endif
-       return RUN_ALL_TESTS();
-#else
-   
-   throw GtestMissingError();
 #endif
-}
 
+#include "../../main_mpi.h"
diff --git a/src/UnitTests/Meshes/DistributedMeshes/CutMeshFunctionTest.cpp b/src/UnitTests/Meshes/DistributedMeshes/CutMeshFunctionTest.cpp
index ce78b85680..854cdbda1e 100644
--- a/src/UnitTests/Meshes/DistributedMeshes/CutMeshFunctionTest.cpp
+++ b/src/UnitTests/Meshes/DistributedMeshes/CutMeshFunctionTest.cpp
@@ -213,13 +213,4 @@ TEST(CutMeshFunction, 3D_2)
 
 #endif
 
-#include "../../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../../main.h"
diff --git a/src/UnitTests/Meshes/DistributedMeshes/DirectionsTest.cpp b/src/UnitTests/Meshes/DistributedMeshes/DirectionsTest.cpp
index b772ecda06..bc1f94f8af 100644
--- a/src/UnitTests/Meshes/DistributedMeshes/DirectionsTest.cpp
+++ b/src/UnitTests/Meshes/DistributedMeshes/DirectionsTest.cpp
@@ -120,15 +120,4 @@ TEST(XYZ, 3D )
 
 #endif
 
-#include "../../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-       int result= RUN_ALL_TESTS();
-       return result;
-#else
-   
-   throw GtestMissingError();
-#endif
-}
+#include "../../main.h"
diff --git a/src/UnitTests/Meshes/DistributedMeshes/DistributedGridIOTestBase.h b/src/UnitTests/Meshes/DistributedMeshes/DistributedGridIOTestBase.h
index 537bcd9239..cd4a155ef6 100644
--- a/src/UnitTests/Meshes/DistributedMeshes/DistributedGridIOTestBase.h
+++ b/src/UnitTests/Meshes/DistributedMeshes/DistributedGridIOTestBase.h
@@ -12,7 +12,6 @@
 #ifdef HAVE_MPI
 
 #include "DistributedGridIOTest.h"
-#include <TNL/Communicators/ScopedInitializer.h>
 
 TEST( DistributedGridIO, Save_1D )
 {
@@ -74,74 +73,10 @@ TEST( DistributedGridIO, Load_3D_GPU )
 {
     TestDistributedGridIO<3,Cuda>::TestLoad();
 }
-
-#endif
-
-#else
-TEST(NoMPI, NoTest)
-{
-    ASSERT_TRUE(true) << ":-(";
-}
 #endif
 
 #endif
 
-#if (defined(HAVE_GTEST) && defined(HAVE_MPI))
-#include <sstream>
-
-  class MinimalistBufferedPrinter : public ::testing::EmptyTestEventListener {
-      
-  private:
-      std::stringstream sout;
-      
-  public:
-      
-    // Called before a test starts.
-    virtual void OnTestStart(const ::testing::TestInfo& test_info) {
-      sout<< test_info.test_case_name() <<"." << test_info.name() << " Start." <<std::endl;
-    }
-
-    // Called after a failed assertion or a SUCCEED() invocation.
-    virtual void OnTestPartResult(
-        const ::testing::TestPartResult& test_part_result) {
-      sout << (test_part_result.failed() ? "====Failure=== " : "===Success=== ") 
-              << test_part_result.file_name() << " "
-              << test_part_result.line_number() <<std::endl
-              << test_part_result.summary() <<std::endl;
-    }
-
-    // Called after a test ends.
-    virtual void OnTestEnd(const ::testing::TestInfo& test_info) 
-    {
-        int rank=CommunicatorType::GetRank(CommunicatorType::AllGroup);
-        sout<< test_info.test_case_name() <<"." << test_info.name() << " End." <<std::endl;
-        std::cout << rank << ":" << std::endl << sout.str()<< std::endl;
-        sout.str( std::string() );
-        sout.clear();
-    }
-  };
 #endif
 
-#include "../../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-
-    #ifdef HAVE_MPI
-       ::testing::TestEventListeners& listeners =
-          ::testing::UnitTest::GetInstance()->listeners();
-
-       delete listeners.Release(listeners.default_result_printer());
-       listeners.Append(new MinimalistBufferedPrinter);
-
-       Communicators::ScopedInitializer< CommunicatorType > mpi(argc, argv);
-       CommunicatorType::setRedirection( false );
-       CommunicatorType::setupRedirection();
-    #endif
-       return RUN_ALL_TESTS();
-#else
-   
-   throw GtestMissingError();
-#endif
-}
+#include "../../main_mpi.h"
diff --git a/src/UnitTests/Meshes/DistributedMeshes/DistributedGridIO_MPIIOTestBase.h b/src/UnitTests/Meshes/DistributedMeshes/DistributedGridIO_MPIIOTestBase.h
index 4e3603a7a4..f84de9065b 100644
--- a/src/UnitTests/Meshes/DistributedMeshes/DistributedGridIO_MPIIOTestBase.h
+++ b/src/UnitTests/Meshes/DistributedMeshes/DistributedGridIO_MPIIOTestBase.h
@@ -10,7 +10,6 @@
 #ifdef HAVE_MPI
 
 #include "DistributedGridIO_MPIIOTest.h"
-#include <TNL/Communicators/ScopedInitializer.h>
 
 TEST( DistributedGridMPIIO, Save_1D )
 {
@@ -74,71 +73,8 @@ TEST( DistributedGridMPIIO, Load_3D )
     }
 #endif
 
-#else
-TEST(NoMPI, NoTest)
-{
-    ASSERT_TRUE(true) << ":-(";
-}
-#endif
-
 #endif
 
-#if (defined(HAVE_GTEST) && defined(HAVE_MPI))
-#include <sstream>
-
-  class MinimalistBufferedPrinter : public ::testing::EmptyTestEventListener {
-      
-  private:
-      std::stringstream sout;
-      
-  public:
-      
-    // Called before a test starts.
-    virtual void OnTestStart(const ::testing::TestInfo& test_info) {
-      sout<< test_info.test_case_name() <<"." << test_info.name() << " Start." <<std::endl;
-    }
-
-    // Called after a failed assertion or a SUCCEED() invocation.
-    virtual void OnTestPartResult(
-        const ::testing::TestPartResult& test_part_result) {
-      sout << (test_part_result.failed() ? "====Failure=== " : "===Success=== ") 
-              << test_part_result.file_name() << " "
-              << test_part_result.line_number() <<std::endl
-              << test_part_result.summary() <<std::endl;
-    }
-
-    // Called after a test ends.
-    virtual void OnTestEnd(const ::testing::TestInfo& test_info) 
-    {
-        int rank=CommunicatorType::GetRank(CommunicatorType::AllGroup);
-        sout<< test_info.test_case_name() <<"." << test_info.name() << " End." <<std::endl;
-        std::cout << rank << ":" << std::endl << sout.str()<< std::endl;
-        sout.str( std::string() );
-        sout.clear();
-    }
-  };
 #endif
 
-#include "../../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-
-    #ifdef HAVE_MPI
-       ::testing::TestEventListeners& listeners =
-          ::testing::UnitTest::GetInstance()->listeners();
-
-       delete listeners.Release(listeners.default_result_printer());
-       listeners.Append(new MinimalistBufferedPrinter);
-
-       Communicators::ScopedInitializer< CommunicatorType > mpi(argc, argv);
-       CommunicatorType::setRedirection( false );
-       CommunicatorType::setupRedirection();
-    #endif
-       return RUN_ALL_TESTS();
-#else
-   
-   throw GtestMissingError();
-#endif
-}
+#include "../../main_mpi.h"
diff --git a/src/UnitTests/Meshes/DistributedMeshes/DistributedGridTest_1D.cpp b/src/UnitTests/Meshes/DistributedMeshes/DistributedGridTest_1D.cpp
index 43af001612..1a5dcb8194 100644
--- a/src/UnitTests/Meshes/DistributedMeshes/DistributedGridTest_1D.cpp
+++ b/src/UnitTests/Meshes/DistributedMeshes/DistributedGridTest_1D.cpp
@@ -13,7 +13,6 @@
 #ifdef HAVE_MPI    
 
 #include <TNL/Communicators/MpiCommunicator.h>
-#include <TNL/Communicators/ScopedInitializer.h>
 #include <TNL/Functions/MeshFunction.h>
 #include <TNL/Meshes/DistributedMeshes/DistributedMesh.h>
 #include <TNL/Meshes/DistributedMeshes/SubdomainOverlapsGetter.h>
@@ -378,71 +377,8 @@ TEST_F(DistributedGridTest_1D, SynchronizePeriodicBoundariesLinearTest )
    if( rank == nproc - 1 )
       EXPECT_EQ( meshFunctionPtr->getValue(entity2), 0 ) << "Linear function Overlap error on right Edge.";
 }
-
-#else
-TEST(NoMPI, NoTest)
-{
-    ASSERT_TRUE(true) << ":-(";
-}
 #endif
 
 #endif
 
-
-#if (defined(HAVE_GTEST) && defined(HAVE_MPI))
-#include <sstream>
-
-  class MinimalistBufferedPrinter : public ::testing::EmptyTestEventListener {
-      
-  private:
-      std::stringstream sout;
-      
-  public:
-      
-    // Called before a test starts.
-    virtual void OnTestStart(const ::testing::TestInfo& test_info) {
-      sout<< test_info.test_case_name() <<"." << test_info.name() << " Start." <<std::endl;
-    }
-
-    // Called after a failed assertion or a SUCCEED() invocation.
-    virtual void OnTestPartResult(
-        const ::testing::TestPartResult& test_part_result) {
-      sout << (test_part_result.failed() ? "====Failure=== " : "===Success=== ") 
-              << test_part_result.file_name() << " "
-              << test_part_result.line_number() <<std::endl
-              << test_part_result.summary() <<std::endl;
-    }
-
-    // Called after a test ends.
-    virtual void OnTestEnd(const ::testing::TestInfo& test_info) 
-    {
-		int rank=CommunicatorType::GetRank(CommunicatorType::AllGroup);
-        sout<< test_info.test_case_name() <<"." << test_info.name() << " End." <<std::endl;
-        std::cout << rank << ":" << std::endl << sout.str()<< std::endl;
-        sout.str( std::string() );
-        sout.clear();
-    }
-  };
-#endif
-
-#include "../../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-
-    #ifdef HAVE_MPI
-       ::testing::TestEventListeners& listeners =
-          ::testing::UnitTest::GetInstance()->listeners();
-
-       delete listeners.Release(listeners.default_result_printer());
-       listeners.Append(new MinimalistBufferedPrinter);
-
-       Communicators::ScopedInitializer< CommunicatorType > mpi(argc, argv);
-    #endif
-       return RUN_ALL_TESTS();
-#else
-   
-   throw GtestMissingError();
-#endif
-}
+#include "../../main_mpi.h"
diff --git a/src/UnitTests/Meshes/DistributedMeshes/DistributedGridTest_2D.cpp b/src/UnitTests/Meshes/DistributedMeshes/DistributedGridTest_2D.cpp
index f5a0056de7..2aaeaa06be 100644
--- a/src/UnitTests/Meshes/DistributedMeshes/DistributedGridTest_2D.cpp
+++ b/src/UnitTests/Meshes/DistributedMeshes/DistributedGridTest_2D.cpp
@@ -15,7 +15,6 @@
 #include <TNL/Meshes/DistributedMeshes/DistributedMesh.h>
 #include <TNL/Functions/MeshFunction.h>
 #include <TNL/Communicators/MpiCommunicator.h>
-#include <TNL/Communicators/ScopedInitializer.h>
 #include <TNL/Meshes/DistributedMeshes/SubdomainOverlapsGetter.h>
 
 #include "../../Functions/Functions.h"
@@ -1017,73 +1016,8 @@ TEST_F(DistributedGridTest_2D, SynchronizerNeighborPeriodicBoundariesWithInActiv
    }
 }
 */ 
-
-#else
-TEST(NoMPI, NoTest)
-{
-    ASSERT_TRUE(true) << ":-(";
-}
 #endif
 
 #endif
 
-
-#if (defined(HAVE_GTEST) && defined(HAVE_MPI))
-#include <sstream>
-
-  class MinimalistBufferedPrinter : public ::testing::EmptyTestEventListener {
-      
-  private:
-      std::stringstream sout;
-      
-  public:
-      
-    // Called before a test starts.
-    virtual void OnTestStart(const ::testing::TestInfo& test_info) {
-      sout<< test_info.test_case_name() <<"." << test_info.name() << " Start." <<std::endl;
-    }
-
-    // Called after a failed assertion or a SUCCEED() invocation.
-    virtual void OnTestPartResult(
-        const ::testing::TestPartResult& test_part_result) {
-      sout << (test_part_result.failed() ? "====Failure=== " : "===Success=== ") 
-              << test_part_result.file_name() << " "
-              << test_part_result.line_number() <<std::endl
-              << test_part_result.summary() <<std::endl;
-    }
-
-    // Called after a test ends.
-    virtual void OnTestEnd(const ::testing::TestInfo& test_info) 
-    {
-        int rank=CommunicatorType::GetRank(CommunicatorType::AllGroup);
-        sout<< test_info.test_case_name() <<"." << test_info.name() << " End." <<std::endl;
-        std::cout << rank << ":" << std::endl << sout.str()<< std::endl;
-        sout.str( std::string() );
-        sout.clear();
-    }
-  };
-#endif
-
-#include "../../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-
-    #ifdef HAVE_MPI
-       ::testing::TestEventListeners& listeners =
-          ::testing::UnitTest::GetInstance()->listeners();
-
-       delete listeners.Release(listeners.default_result_printer());
-       listeners.Append(new MinimalistBufferedPrinter);
-
-       Communicators::ScopedInitializer< CommunicatorType > mpi(argc, argv);
-    #endif
-       return RUN_ALL_TESTS();
-#else
-   
-   throw GtestMissingError();
-#endif
-}
-
-
+#include "../../main_mpi.h"
diff --git a/src/UnitTests/Meshes/DistributedMeshes/DistributedGridTest_3D.cpp b/src/UnitTests/Meshes/DistributedMeshes/DistributedGridTest_3D.cpp
index f92ef08d7a..823c30db92 100644
--- a/src/UnitTests/Meshes/DistributedMeshes/DistributedGridTest_3D.cpp
+++ b/src/UnitTests/Meshes/DistributedMeshes/DistributedGridTest_3D.cpp
@@ -4,7 +4,6 @@
 #ifdef HAVE_MPI    
 
 #include <TNL/Communicators/MpiCommunicator.h>
-#include <TNL/Communicators/ScopedInitializer.h>
 #include <TNL/Functions/MeshFunction.h>
 #include <TNL/Meshes/DistributedMeshes/DistributedMesh.h>
 #include <TNL/Meshes/DistributedMeshes/SubdomainOverlapsGetter.h>
@@ -720,75 +719,8 @@ TEST_F(DistributedGirdTest_3D, SynchronizerNeighborTest)
 
 }
 */
-
-#else
-TEST(NoMPI, NoTest)
-{
-    ASSERT_TRUE(true) << ":-(";
-}
-#endif
-
-#endif
-
-
-#if (defined(HAVE_GTEST) && defined(HAVE_MPI))
-#include <sstream>
-
-  class MinimalistBufferedPrinter : public ::testing::EmptyTestEventListener {
-      
-  private:
-      std::stringstream sout;
-      
-  public:
-      
-    // Called before a test starts.
-    virtual void OnTestStart(const ::testing::TestInfo& test_info) {
-      sout<< test_info.test_case_name() <<"." << test_info.name() << " Start." <<std::endl;
-    }
-
-    // Called after a failed assertion or a SUCCEED() invocation.
-    virtual void OnTestPartResult(
-        const ::testing::TestPartResult& test_part_result) {
-      sout << (test_part_result.failed() ? "====Failure=== " : "===Success=== ") 
-              << test_part_result.file_name() << " "
-              << test_part_result.line_number() <<std::endl
-              << test_part_result.summary() <<std::endl;
-    }
-
-    // Called after a test ends.
-    virtual void OnTestEnd(const ::testing::TestInfo& test_info) 
-    {
-        int rank=CommunicatorType::GetRank(CommunicatorType::AllGroup);
-        sout<< test_info.test_case_name() <<"." << test_info.name() << " End." <<std::endl;
-        std::cout << rank << ":" << std::endl << sout.str()<< std::endl;
-        sout.str( std::string() );
-        sout.clear();
-    }
-  };
 #endif
 
-#include "../../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-
-    #ifdef HAVE_MPI
-       ::testing::TestEventListeners& listeners =
-          ::testing::UnitTest::GetInstance()->listeners();
-
-       delete listeners.Release(listeners.default_result_printer());
-       listeners.Append(new MinimalistBufferedPrinter);
-
-       Communicators::ScopedInitializer< CommunicatorType > mpi(argc, argv);
-    #endif
-       return RUN_ALL_TESTS();
-#else
-   
-   throw GtestMissingError();
 #endif
-}
-
-
-
 
+#include "../../main_mpi.h"
diff --git a/src/UnitTests/Meshes/DistributedMeshes/DistributedVectorFieldIO_MPIIOTest.cpp b/src/UnitTests/Meshes/DistributedMeshes/DistributedVectorFieldIO_MPIIOTest.cpp
index 67098fc5db..0a5ab3e37d 100644
--- a/src/UnitTests/Meshes/DistributedMeshes/DistributedVectorFieldIO_MPIIOTest.cpp
+++ b/src/UnitTests/Meshes/DistributedMeshes/DistributedVectorFieldIO_MPIIOTest.cpp
@@ -1,12 +1,8 @@
-
-
-
 #ifdef HAVE_GTEST
       #include <gtest/gtest.h>
 #ifdef HAVE_MPI
 
 #include <TNL/Communicators/MpiCommunicator.h>
-#include <TNL/Communicators/ScopedInitializer.h>
 #include "DistributedVectorFieldIO_MPIIOTestBase.h"
 
 using namespace TNL::Communicators;
@@ -43,73 +39,8 @@ TEST( DistributedVectorFieldIO_MPIIO, Load_3D )
 {
     TestDistributedVectorFieldMPIIO<3,2,Host>::TestLoad();
 }
-
-
-#else
-TEST(NoMPI, NoTest)
-{
-    ASSERT_TRUE(true) << ":-(";
-}
 #endif
 
 #endif
 
-#if (defined(HAVE_GTEST) && defined(HAVE_MPI))
-#include <sstream>
-
-  class MinimalistBufferedPrinter : public ::testing::EmptyTestEventListener {
-      
-  private:
-      std::stringstream sout;
-      
-  public:
-      
-    // Called before a test starts.
-    virtual void OnTestStart(const ::testing::TestInfo& test_info) {
-      sout<< test_info.test_case_name() <<"." << test_info.name() << " Start." <<std::endl;
-    }
-
-    // Called after a failed assertion or a SUCCEED() invocation.
-    virtual void OnTestPartResult(
-        const ::testing::TestPartResult& test_part_result) {
-      sout << (test_part_result.failed() ? "====Failure=== " : "===Success=== ") 
-              << test_part_result.file_name() << " "
-              << test_part_result.line_number() <<std::endl
-              << test_part_result.summary() <<std::endl;
-    }
-
-    // Called after a test ends.
-    virtual void OnTestEnd(const ::testing::TestInfo& test_info) 
-    {
-        int rank=CommunicatorType::GetRank(CommunicatorType::AllGroup);
-        sout<< test_info.test_case_name() <<"." << test_info.name() << " End." <<std::endl;
-        std::cout << rank << ":" << std::endl << sout.str()<< std::endl;
-        sout.str( std::string() );
-        sout.clear();
-    }
-  };
-#endif
-
-#include "../../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-
-    #ifdef HAVE_MPI
-       ::testing::TestEventListeners& listeners =
-          ::testing::UnitTest::GetInstance()->listeners();
-
-       delete listeners.Release(listeners.default_result_printer());
-       listeners.Append(new MinimalistBufferedPrinter);
-
-       Communicators::ScopedInitializer< CommunicatorType > mpi(argc, argv);
-       CommunicatorType::setRedirection( false );
-       CommunicatorType::setupRedirection();
-    #endif
-       return RUN_ALL_TESTS();
-#else
-   
-   throw GtestMissingError();
-#endif
-}
+#include "../../main_mpi.h"
diff --git a/src/UnitTests/Meshes/MeshEntityTest.cpp b/src/UnitTests/Meshes/MeshEntityTest.cpp
index e3f87a32bf..66b6da128f 100644
--- a/src/UnitTests/Meshes/MeshEntityTest.cpp
+++ b/src/UnitTests/Meshes/MeshEntityTest.cpp
@@ -1,11 +1,2 @@
 #include "MeshEntityTest.h"
-
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   return EXIT_FAILURE;
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Meshes/MeshOrderingTest.cpp b/src/UnitTests/Meshes/MeshOrderingTest.cpp
index fc03c92cc7..ff02fe8a2c 100644
--- a/src/UnitTests/Meshes/MeshOrderingTest.cpp
+++ b/src/UnitTests/Meshes/MeshOrderingTest.cpp
@@ -1,11 +1,2 @@
 #include "MeshOrderingTest.h"
-
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   return EXIT_FAILURE;
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Meshes/MeshOrderingTest.cu b/src/UnitTests/Meshes/MeshOrderingTest.cu
index fc03c92cc7..ff02fe8a2c 100644
--- a/src/UnitTests/Meshes/MeshOrderingTest.cu
+++ b/src/UnitTests/Meshes/MeshOrderingTest.cu
@@ -1,11 +1,2 @@
 #include "MeshOrderingTest.h"
-
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   return EXIT_FAILURE;
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Meshes/MeshTest.cpp b/src/UnitTests/Meshes/MeshTest.cpp
index 93004f18d5..d0ee1e52f4 100644
--- a/src/UnitTests/Meshes/MeshTest.cpp
+++ b/src/UnitTests/Meshes/MeshTest.cpp
@@ -1,11 +1,2 @@
 #include "MeshTest.h"
-
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   return EXIT_FAILURE;
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Meshes/MeshTest.cu b/src/UnitTests/Meshes/MeshTest.cu
index 93004f18d5..d0ee1e52f4 100644
--- a/src/UnitTests/Meshes/MeshTest.cu
+++ b/src/UnitTests/Meshes/MeshTest.cu
@@ -1,11 +1,2 @@
 #include "MeshTest.h"
-
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   return EXIT_FAILURE;
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/ObjectTest.cpp b/src/UnitTests/ObjectTest.cpp
index 8de4175078..be30000b20 100644
--- a/src/UnitTests/ObjectTest.cpp
+++ b/src/UnitTests/ObjectTest.cpp
@@ -91,18 +91,6 @@ TEST( HeaderTest, SaveAndLoadTest )
 
    EXPECT_EQ( std::remove( "test-file.tnl" ), 0 );
 }
-
-
 #endif
 
-
-#include "GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "main.h"
diff --git a/src/UnitTests/Pointers/SharedPointerCudaTest.cu b/src/UnitTests/Pointers/SharedPointerCudaTest.cu
index 813054c140..c0d76b2cc0 100644
--- a/src/UnitTests/Pointers/SharedPointerCudaTest.cu
+++ b/src/UnitTests/Pointers/SharedPointerCudaTest.cu
@@ -14,12 +14,11 @@
 #include <TNL/Containers/StaticArray.h>
 #include <TNL/Containers/Array.h>
 
-#ifdef HAVE_GTEST 
+#ifdef HAVE_GTEST
 #include <gtest/gtest.h>
 #endif
 
 #include <TNL/Devices/Cuda.h>
-#include "../GtestMissingError.h"
 
 using namespace TNL;
 
@@ -50,18 +49,18 @@ TEST( SharedPointerCudaTest, getDataTest )
 #ifdef HAVE_CUDA
    typedef TNL::Containers::StaticArray< 2, int  > TestType;
    Pointers::SharedPointer< TestType, Devices::Cuda > ptr1( 1, 2 );
-   
+
 #ifdef HAVE_CUDA_UNIFIED_MEMORY
    ASSERT_EQ( ptr1->x(), 1 );
    ASSERT_EQ( ptr1->y(), 2 );
 #else
-   
+
    Devices::Cuda::synchronizeDevice();
-   
+
    TestType aux;
-   
+
    cudaMemcpy( ( void*) &aux, &ptr1.getData< Devices::Cuda >(), sizeof( TestType ), cudaMemcpyDeviceToHost );
-   
+
    ASSERT_EQ( aux[ 0 ], 1 );
    ASSERT_EQ( aux[ 1 ], 2 );
 #endif  // HAVE_CUDA_UNIFIED_MEMORY
@@ -85,7 +84,7 @@ TEST( SharedPointerCudaTest, getDataArrayTest )
 #ifdef HAVE_CUDA
    typedef TNL::Containers::Array< int, Devices::Cuda  > TestType;
    Pointers::SharedPointer< TestType > ptr;
-   
+
    ptr->setSize( 2 );
    ptr->setElement( 0, 1 );
    ptr->setElement( 1, 2 );
@@ -97,10 +96,10 @@ TEST( SharedPointerCudaTest, getDataArrayTest )
    copyArrayKernel<<< 1, 2 >>>( &ptr.getData< Devices::Cuda >(), testArray_device );
    testArray_host = new int [ 2 ];
    cudaMemcpy( testArray_host, testArray_device, 2 * sizeof( int ), cudaMemcpyDeviceToHost );
-   
+
    ASSERT_EQ( testArray_host[ 0 ], 1 );
    ASSERT_EQ( testArray_host[ 1 ], 2 );
-   
+
    delete[] testArray_host;
    cudaFree( testArray_device );
 
@@ -112,10 +111,10 @@ TEST( SharedPointerCudaTest, nullptrAssignement )
 #ifdef HAVE_CUDA
    using TestType = Pointers::SharedPointer< double, Devices::Cuda >;
    TestType p1( 5 ), p2( nullptr );
-   
+
    // This should not crash
    p1 = p2;
-   
+
    ASSERT_FALSE( p1 );
    ASSERT_FALSE( p2 );
 #endif
@@ -126,23 +125,15 @@ TEST( SharedPointerCudaTest, swap )
 #ifdef HAVE_CUDA
    using TestType = Pointers::SharedPointer< double, Devices::Cuda >;
    TestType p1( 1 ), p2( 2 );
-   
+
    p1.swap( p2 );
-   
+
    ASSERT_EQ( *p1, 2 );
    ASSERT_EQ( *p2, 1 );
 #endif
 }
 
-
 #endif
 
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+
+#include "../main.h"
diff --git a/src/UnitTests/Pointers/SharedPointerHostTest.cpp b/src/UnitTests/Pointers/SharedPointerHostTest.cpp
index b7d441f70e..53501e774b 100644
--- a/src/UnitTests/Pointers/SharedPointerHostTest.cpp
+++ b/src/UnitTests/Pointers/SharedPointerHostTest.cpp
@@ -13,13 +13,13 @@
 #include <TNL/Pointers/SharedPointer.h>
 #include <TNL/Containers/StaticArray.h>
 
-#ifdef HAVE_GTEST 
+#ifdef HAVE_GTEST
 #include <gtest/gtest.h>
 #endif
 
 using namespace TNL;
 
-#ifdef HAVE_GTEST 
+#ifdef HAVE_GTEST
 TEST( SharedPointerHostTest, ConstructorTest )
 {
    typedef TNL::Containers::StaticArray< 2, int  > TestType;
@@ -43,10 +43,10 @@ TEST( SharedPointerCudaTest, nullptrAssignement )
 {
    using TestType = Pointers::SharedPointer< double, Devices::Host >;
    TestType p1( 5 ), p2( nullptr );
-   
+
    // This should not crash
    p1 = p2;
-   
+
    ASSERT_FALSE( p1 );
    ASSERT_FALSE( p2 );
 }
@@ -55,22 +55,12 @@ TEST( SharedPointerCudaTest, swap )
 {
    using TestType = Pointers::SharedPointer< double, Devices::Host >;
    TestType p1( 1 ), p2( 2 );
-   
+
    p1.swap( p2 );
-   
+
    ASSERT_EQ( *p1, 2 );
    ASSERT_EQ( *p2, 1 );
 }
-
 #endif
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/Pointers/UniquePointerTest.cpp b/src/UnitTests/Pointers/UniquePointerTest.cpp
index 2fba352f58..4473580171 100644
--- a/src/UnitTests/Pointers/UniquePointerTest.cpp
+++ b/src/UnitTests/Pointers/UniquePointerTest.cpp
@@ -13,14 +13,14 @@
 #include <TNL/Pointers/UniquePointer.h>
 #include <TNL/Containers/StaticArray.h>
 
-#ifdef HAVE_GTEST 
+#ifdef HAVE_GTEST
 #include <gtest/gtest.h>
 #endif
 
 using namespace TNL;
 using namespace TNL::Pointers;
 
-#ifdef HAVE_GTEST 
+#ifdef HAVE_GTEST
 TEST( UniquePointerTest, ConstructorTest )
 {
    typedef TNL::Containers::StaticArray< 2, int  > TestType;
@@ -41,13 +41,4 @@ TEST( UniquePointerTest, ConstructorTest )
 };
 #endif
 
-#include "../GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
+#include "../main.h"
diff --git a/src/UnitTests/SaveAndLoadMeshfunctionTest.cpp b/src/UnitTests/SaveAndLoadMeshfunctionTest.cpp
index 9e4b1eaf36..1596fbcfbe 100644
--- a/src/UnitTests/SaveAndLoadMeshfunctionTest.cpp
+++ b/src/UnitTests/SaveAndLoadMeshfunctionTest.cpp
@@ -108,18 +108,6 @@ TEST( CopyEntitiesTest, 3D )
 {
     TestSaveAndLoadMeshfunction<3>::Test();
 }
-
-
 #endif
 
-#include "GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
-
+#include "main.h"
diff --git a/src/UnitTests/StringTest.cpp b/src/UnitTests/StringTest.cpp
index 755cb6419d..099bc4d6c4 100644
--- a/src/UnitTests/StringTest.cpp
+++ b/src/UnitTests/StringTest.cpp
@@ -316,17 +316,6 @@ TEST( StringTest, SaveLoad )
 
    EXPECT_EQ( std::remove( "test-file.tnl" ), 0 );
 };
-
-#endif
-
-#include "GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
 #endif
-}
 
+#include "main.h"
diff --git a/src/UnitTests/TimerTest.cpp b/src/UnitTests/TimerTest.cpp
index 6c20c4abb2..dd46f30f3c 100644
--- a/src/UnitTests/TimerTest.cpp
+++ b/src/UnitTests/TimerTest.cpp
@@ -10,7 +10,7 @@
 
 // Implemented by Nina Dzugasova
 
-#ifdef HAVE_GTEST 
+#ifdef HAVE_GTEST
 #include <gtest/gtest.h>
 #endif
 
@@ -18,7 +18,7 @@
 
 using namespace TNL;
 
-#ifdef HAVE_GTEST 
+#ifdef HAVE_GTEST
 TEST( TimerTest, Constructor )
 {
     Timer time;
@@ -34,16 +34,4 @@ TEST( TimerTest, Constructor )
 }
 #endif
 
-
-#include "GtestMissingError.h"
-int main( int argc, char* argv[] )
-{
-#ifdef HAVE_GTEST
-   ::testing::InitGoogleTest( &argc, argv );
-   return RUN_ALL_TESTS();
-#else
-   throw GtestMissingError();
-#endif
-}
-
-
+#include "main.h"
diff --git a/src/UnitTests/main.h b/src/UnitTests/main.h
new file mode 100644
index 0000000000..87da37fa89
--- /dev/null
+++ b/src/UnitTests/main.h
@@ -0,0 +1,15 @@
+#ifdef HAVE_GTEST
+#include <gtest/gtest.h>
+#else
+#include "GtestMissingError.h"
+#endif
+
+int main( int argc, char* argv[] )
+{
+#ifdef HAVE_GTEST
+   ::testing::InitGoogleTest( &argc, argv );
+   return RUN_ALL_TESTS();
+#else
+   throw GtestMissingError();
+#endif
+}
diff --git a/src/UnitTests/main_mpi.h b/src/UnitTests/main_mpi.h
new file mode 100644
index 0000000000..3c7c39a86f
--- /dev/null
+++ b/src/UnitTests/main_mpi.h
@@ -0,0 +1,66 @@
+#ifdef HAVE_GTEST
+#include <gtest/gtest.h>
+#else
+#include "GtestMissingError.h"
+#endif
+
+#if (defined(HAVE_GTEST) && defined(HAVE_MPI))
+#include <TNL/Communicators/MpiCommunicator.h>
+#include <TNL/Communicators/ScopedInitializer.h>
+using CommunicatorType = Communicators::MpiCommunicator;
+
+#include <sstream>
+
+class MinimalistBufferedPrinter
+: public ::testing::EmptyTestEventListener
+{
+private:
+   std::stringstream sout;
+
+public:
+   // Called before a test starts.
+   virtual void OnTestStart(const ::testing::TestInfo& test_info)
+   {
+      sout << test_info.test_case_name() << "." << test_info.name() << " Start." << std::endl;
+   }
+
+   // Called after a failed assertion or a SUCCEED() invocation.
+   virtual void OnTestPartResult(const ::testing::TestPartResult& test_part_result)
+   {
+      sout << (test_part_result.failed() ? "====Failure=== " : "===Success=== ")
+           << test_part_result.file_name() << " "
+           << test_part_result.line_number() <<std::endl
+           << test_part_result.summary() <<std::endl;
+   }
+
+   // Called after a test ends.
+   virtual void OnTestEnd(const ::testing::TestInfo& test_info)
+   {
+      const int rank = CommunicatorType::GetRank(CommunicatorType::AllGroup);
+      sout << test_info.test_case_name() << "." << test_info.name() << " End." <<std::endl;
+      std::cout << rank << ":" << std::endl << sout.str()<< std::endl;
+      sout.str( std::string() );
+      sout.clear();
+   }
+};
+#endif
+
+int main( int argc, char* argv[] )
+{
+#ifdef HAVE_GTEST
+   ::testing::InitGoogleTest( &argc, argv );
+
+   #ifdef HAVE_MPI
+      ::testing::TestEventListeners& listeners =
+         ::testing::UnitTest::GetInstance()->listeners();
+
+      delete listeners.Release(listeners.default_result_printer());
+      listeners.Append(new MinimalistBufferedPrinter);
+
+      Communicators::ScopedInitializer< CommunicatorType > mpi(argc, argv);
+   #endif
+   return RUN_ALL_TESTS();
+#else
+   throw GtestMissingError();
+#endif
+}
-- 
GitLab