GoogleTest : 테스트를 건너 뛰는 방법?
Google Test 1.6 (Windows 7, Visual Studio C ++) 사용. 주어진 테스트를 어떻게 끌 수 있습니까? (일명 테스트 실행을 막을 수있는 방법). 전체 테스트를 주석 처리하는 것 외에 내가 할 수있는 일이 있습니까?
"즉시 수정할 수없는 손상된 테스트가있는 경우 이름에 DISABLED_ 접두사를 추가 할 수 있습니다. 이렇게하면 실행에서 제외됩니다."
예 :
// Tests that Foo does Abc.
TEST(FooTest, DISABLED_DoesAbc) { ... }
class DISABLED_BarTest : public ::testing::Test { ... };
// Tests that Bar does Xyz.
TEST_F(DISABLED_BarTest, DoesXyz) { ... }
문서에 따라 테스트 하위 집합을 실행할 수도 있습니다 .
테스트의 하위 집합 실행
기본적으로 Google 테스트 프로그램은 사용자가 정의한 모든 테스트를 실행합니다. 때로는 테스트의 하위 집합 만 실행하고 싶을 때가 있습니다 (예 : 디버깅 또는 변경 사항을 신속하게 확인하기 위해). GTEST_FILTER 환경 변수 또는 --gtest_filter 플래그를 필터 문자열로 설정하면 Google 테스트는 전체 이름 (TestCaseName.TestName 형식)이 필터와 일치하는 테스트 만 실행합니다.
필터의 형식은 ':'로 구분 된 와일드 카드 패턴 목록 (양수 패턴이라고 함)이며 선택적으로 '-'및 다른 ':'로 구분 된 패턴 목록 (음수 패턴이라고 함)이 뒤 따릅니다. 테스트는 긍정적 인 패턴과 일치하지만 부정적인 패턴과 일치하지 않는 경우에만 필터와 일치합니다.
패턴에는 '*'(모든 문자열과 일치) 또는 '?'가 포함될 수 있습니다. (모든 단일 문자와 일치). 편의상 '* -NegativePatterns'필터는 '-NegativePatterns'로도 쓸 수 있습니다.
예를 들면 :
./foo_test Has no flag, and thus runs all its tests. ./foo_test --gtest_filter=* Also runs everything, due to the single match-everything * value. ./foo_test --gtest_filter=FooTest.* Runs everything in test case FooTest. ./foo_test --gtest_filter=*Null*:*Constructor* Runs any test whose full name contains either "Null" or "Constructor". ./foo_test --gtest_filter=-*DeathTest.* Runs all non-death tests. ./foo_test --gtest_filter=FooTest.*-FooTest.Bar Runs everything in test case FooTest except FooTest.Bar.
가장 예쁜 솔루션은 아니지만 작동합니다.
다음은 이름에 foo1 또는 foo2 문자열이있는 테스트를 포함하고 이름에 bar1 또는 bar2 문자열이있는 테스트를 제외하는 표현식입니다.
--gtest_filter=*foo1*:*foo2*-*bar1*:*bar2*
나는 그것을 코드로하는 것을 선호한다 :
// Run a specific test only
//testing::GTEST_FLAG(filter) = "MyLibrary.TestReading"; // I'm testing a new feature, run something quickly
// Exclude a specific test
testing::GTEST_FLAG(filter) = "-MyLibrary.TestWriting"; // The writing test is broken, so skip it
모든 테스트를 실행하기 위해 두 줄을 모두 주석 처리하거나, 조사 / 작업중인 단일 기능을 테스트하기 위해 첫 번째 줄의 주석 처리를 제거하거나, 테스트가 중단되었지만 다른 모든 것을 테스트하고 싶은 경우 두 번째 줄의 주석 처리를 제거 할 수 있습니다.
와일드 카드를 사용하고 "MyLibrary.TestNetwork *"또는 "-MyLibrary.TestFileSystem *"목록을 작성하여 기능 모음을 테스트 / 제외 할 수도 있습니다.
이제 GTEST_SKIP()매크로를 사용하여 런타임에 테스트를 조건부로 건너 뛸 수 있습니다 . 예를 들면 :
TEST(Foo, Bar)
{
if (blah)
GTEST_SKIP();
...
}
이 기능 은 최신 기능 이므로 사용하려면 GoogleTest 라이브러리를 업데이트해야 할 수 있습니다.
두 개 이상의 테스트를 건너 뛰어야하는 경우
--gtest_filter=-TestName.*:TestName.*TestCase
또 다른 접근 방식으로 테스트를 함수로 래핑하고 런타임에 정상적인 조건부 검사를 사용하여 원하는 경우에만 실행할 수 있습니다.
#include <gtest/gtest.h>
const bool skip_some_test = true;
bool some_test_was_run = false;
void someTest() {
EXPECT_TRUE(!skip_some_test);
some_test_was_run = true;
}
TEST(BasicTest, Sanity) {
EXPECT_EQ(1, 1);
if(!skip_some_test) {
someTest();
EXPECT_TRUE(some_test_was_run);
}
}
This is useful for me as I'm trying to run some tests only when a system supports dual stack IPv6.
Technically that dualstack stuff shouldn't really be a unit test as it depends on the system. But I can't really make any integration tests until I have tested they work anyway and this ensures that it won't report failures when it's not the codes fault.
As for the test of it I have stub objects that simulate a system's support for dualstack (or lack of) by constructing fake sockets.
The only downside is that the test output and the number of tests will change which could cause issues with something that monitors the number of successful tests.
You can also use ASSERT_* rather than EQUAL_*. Assert will about the rest of the test if it fails. Prevents a lot of redundant stuff being dumped to the console.
I had the same need for conditional tests, and I figured out a good workaround. I defined a macro TEST_C that works like a TEST_F macro, but it has a third parameter, which is a boolean expression, evaluated runtime in main.cpp BEFORE the tests are started. Tests that evaluate false are not executed. The macro is ugly, but it look like:
#pragma once
extern std::map<std::string, std::function<bool()> >* m_conditionalTests;
#define TEST_C(test_fixture, test_name, test_condition)\
class test_fixture##_##test_name##_ConditionClass\
{\
public:\
test_fixture##_##test_name##_ConditionClass()\
{\
std::string name = std::string(#test_fixture) + "." + std::string(#test_name);\
if (m_conditionalTests==NULL) {\
m_conditionalTests = new std::map<std::string, std::function<bool()> >();\
}\
m_conditionalTests->insert(std::make_pair(name, []()\
{\
DeviceInfo device = Connection::Instance()->GetDeviceInfo();\
return test_condition;\
}));\
}\
} test_fixture##_##test_name##_ConditionInstance;\
TEST_F(test_fixture, test_name)
Additionally, in your main.cpp, you need this loop to exclude the tests that evaluate false:
// identify tests that cannot run on this device
std::string excludeTests;
for (const auto& exclusion : *m_conditionalTests)
{
bool run = exclusion.second();
if (!run)
{
excludeTests += ":" + exclusion.first;
}
}
// add the exclusion list to gtest
std::string str = ::testing::GTEST_FLAG(filter);
::testing::GTEST_FLAG(filter) = str + ":-" + excludeTests;
// run all tests
int result = RUN_ALL_TESTS();
참고URL : https://stackoverflow.com/questions/7208070/googletest-how-to-skip-a-test
'Programing' 카테고리의 다른 글
| C #에 단 수화-단어를 복수화하는 알고리즘이 있습니까? (0) | 2020.08.16 |
|---|---|
| 단순 HTTP 서버에서 액세스 제어 사용 (0) | 2020.08.16 |
| Rails 애플리케이션의 쿠키 오버플로? (0) | 2020.08.16 |
| Blob을 base64로 변환 (0) | 2020.08.16 |
| 오이 단계 재사용 (0) | 2020.08.16 |