Skip to content

Commit

Permalink
Merge pull request opencv#24186 from dkurt:ts_fixture_constructor_skip
Browse files Browse the repository at this point in the history
Skip test on SkipTestException at fixture's constructor

* Skip test on SkipTestException at fixture's constructor

* Add warning supression

* Skip Python tests if no test file found

* Skip instances of test fixture with exception at SetUpTestCase

* Skip test with exception at SetUp method

* Try remove warning disable

* Add CV_NORETURN

* Remove FAIL assertion

* Use findDataFile to throw Skip exception

* Throw exception conditionally
  • Loading branch information
dkurt committed Aug 25, 2023
1 parent 81cc89a commit 588ddf1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
36 changes: 36 additions & 0 deletions modules/core/test/test_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -917,5 +917,41 @@ REGISTER_TYPED_TEST_CASE_P(Rect_Test, Overflows);
typedef ::testing::Types<int, float, double> RectTypes;
INSTANTIATE_TYPED_TEST_CASE_P(Negative_Test, Rect_Test, RectTypes);

// Expected that SkipTestException thrown in the constructor should skip test but not fail
struct TestFixtureSkip: public ::testing::Test {
TestFixtureSkip(bool throwEx = true) {
if (throwEx) {
throw SkipTestException("Skip test at constructor");
}
}
};

TEST_F(TestFixtureSkip, NoBodyRun) {
FAIL() << "Unreachable code called";
}

// Check no test body started in case of skip exception at static SetUpTestCase
struct TestSetUpTestCaseSkip: public ::testing::Test {
static void SetUpTestCase() {
throw SkipTestException("Skip test at SetUpTestCase");
}
};

TEST_F(TestSetUpTestCaseSkip, NoBodyRun) {
FAIL() << "Unreachable code called";
}
TEST_F(TestSetUpTestCaseSkip, NoBodyRun2) {
FAIL() << "Unreachable code called";
}

struct TestSetUpSkip: public ::testing::Test {
virtual void SetUp() {
throw SkipTestException("Skip test at SetUp");
}
};

TEST_F(TestSetUpSkip, NoBodyRun) {
FAIL() << "Unreachable code called";
}

}} // namespace
2 changes: 2 additions & 0 deletions modules/python/test/tests_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def find_file(self, filename, searchPaths=[], required=True):
return candidate
if required:
self.fail('File ' + filename + ' not found')
else:
self.skipTest('File ' + filename + ' not found')
return None


Expand Down
42 changes: 38 additions & 4 deletions modules/ts/include/opencv2/ts/ts_ext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ bool checkBigDataTests();
} \
} \

#define CV__TEST_SETUP_IMPL(parent_class) \
{ \
try { \
parent_class::SetUp(); \
} catch (const cvtest::details::SkipTestExceptionBase& e) { \
printf("[ SKIP ] %s\n", e.what()); \
} \
}

struct DummyTest : public ::testing::Test {
virtual void TestBody() CV_OVERRIDE {}
};

#undef TEST
#define TEST_(test_case_name, test_name, parent_class, bodyMethodName, BODY_ATTR, BODY_IMPL) \
Expand All @@ -60,6 +72,17 @@ bool checkBigDataTests();
GTEST_DISALLOW_COPY_AND_ASSIGN_(\
GTEST_TEST_CLASS_NAME_(test_case_name, test_name));\
};\
class test_case_name##test_name##_factory : public ::testing::internal::TestFactoryBase { \
public:\
virtual ::testing::Test* CreateTest() { \
try { \
return new GTEST_TEST_CLASS_NAME_(test_case_name, test_name); \
} catch (const cvtest::details::SkipTestExceptionBase& e) { \
printf("[ SKIP ] %s\n", e.what()); \
return new DummyTest(); \
} \
} \
};\
\
::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_case_name, test_name)\
::test_info_ =\
Expand All @@ -69,8 +92,7 @@ bool checkBigDataTests();
(::testing::internal::GetTestTypeId()), \
parent_class::SetUpTestCase, \
parent_class::TearDownTestCase, \
new ::testing::internal::TestFactoryImpl<\
GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>);\
new test_case_name##test_name##_factory);\
void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() BODY_IMPL( #test_case_name "_" #test_name ) \
void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::bodyMethodName()

Expand Down Expand Up @@ -109,10 +131,22 @@ bool checkBigDataTests();
private:\
virtual void TestBody() CV_OVERRIDE;\
virtual void Body(); \
virtual void SetUp() CV_OVERRIDE; \
static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_;\
GTEST_DISALLOW_COPY_AND_ASSIGN_(\
GTEST_TEST_CLASS_NAME_(test_fixture, test_name));\
};\
class test_fixture##test_name##_factory : public ::testing::internal::TestFactoryBase { \
public:\
virtual ::testing::Test* CreateTest() { \
try { \
return new GTEST_TEST_CLASS_NAME_(test_fixture, test_name); \
} catch (const cvtest::details::SkipTestExceptionBase& e) { \
printf("[ SKIP ] %s\n", e.what()); \
return new DummyTest(); \
} \
} \
};\
\
::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_fixture, test_name)\
::test_info_ =\
Expand All @@ -122,9 +156,9 @@ bool checkBigDataTests();
(::testing::internal::GetTypeId<test_fixture>()), \
test_fixture::SetUpTestCase, \
test_fixture::TearDownTestCase, \
new ::testing::internal::TestFactoryImpl<\
GTEST_TEST_CLASS_NAME_(test_fixture, test_name)>);\
new test_fixture##test_name##_factory);\
void GTEST_TEST_CLASS_NAME_(test_fixture, test_name)::TestBody() CV__TEST_BODY_IMPL( #test_fixture "_" #test_name ) \
void GTEST_TEST_CLASS_NAME_(test_fixture, test_name)::SetUp() CV__TEST_SETUP_IMPL(test_fixture) \
void GTEST_TEST_CLASS_NAME_(test_fixture, test_name)::Body()

// Don't use directly
Expand Down
13 changes: 9 additions & 4 deletions modules/ts/src/ts_tags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace cvtest {
static bool printTestTag = false;

static std::vector<std::string> currentDirectTestTags, currentImpliedTestTags;
static std::vector<const ::testing::TestInfo*> skipped_tests;
static std::vector<const ::testing::TestCase*> skipped_tests;

static std::map<std::string, int>& getTestTagsSkipCounts()
{
Expand All @@ -26,7 +26,7 @@ static std::map<std::string, int>& getTestTagsSkipExtraCounts()
void testTagIncreaseSkipCount(const std::string& tag, bool isMain, bool appendSkipTests)
{
if (appendSkipTests)
skipped_tests.push_back(::testing::UnitTest::GetInstance()->current_test_info());
skipped_tests.push_back(::testing::UnitTest::GetInstance()->current_test_case());
std::map<std::string, int>& counts = isMain ? getTestTagsSkipCounts() : getTestTagsSkipExtraCounts();
std::map<std::string, int>::iterator i = counts.find(tag);
if (i == counts.end())
Expand Down Expand Up @@ -280,6 +280,11 @@ static bool isTestTagSkipped(const std::string& testTag, CV_OUT std::string& ski

void checkTestTags()
{
if (std::find(skipped_tests.begin(), skipped_tests.end(),
::testing::UnitTest::GetInstance()->current_test_case()) != skipped_tests.end()) {
throw details::SkipTestExceptionBase(false);
}

std::string skipTag;
const std::vector<std::string>& testTags = currentDirectTestTags;
{
Expand Down Expand Up @@ -307,7 +312,7 @@ void checkTestTags()
}
if (found != tags.size())
{
skipped_tests.push_back(::testing::UnitTest::GetInstance()->current_test_info());
skipped_tests.push_back(::testing::UnitTest::GetInstance()->current_test_case());
throw details::SkipTestExceptionBase("Test tags don't pass required tags list (--test_tag parameter)", true);
}
}
Expand Down Expand Up @@ -341,7 +346,7 @@ void checkTestTags()

if (!skip_message.empty())
{
skipped_tests.push_back(::testing::UnitTest::GetInstance()->current_test_info());
skipped_tests.push_back(::testing::UnitTest::GetInstance()->current_test_case());
throw details::SkipTestExceptionBase(skip_message, true);
}
}
Expand Down

0 comments on commit 588ddf1

Please sign in to comment.