Skip to content

Commit

Permalink
Include skipped message when using GTEST_SKIP macro (#70)
Browse files Browse the repository at this point in the history
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
  • Loading branch information
neyrojasj and nicoddemus committed Dec 3, 2021
1 parent 10fca5c commit 2a2f125
Show file tree
Hide file tree
Showing 23 changed files with 231 additions and 229 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
gtest_ver: [ "1.8.0" ]
gtest_ver: [ "1.11.0" ]

steps:
- uses: actions/checkout@v1
Expand All @@ -22,8 +22,10 @@ jobs:
run: |
wget https://github.com/google/googletest/archive/release-${{ matrix.gtest_ver }}.tar.gz
tar -zxvf release-${{ matrix.gtest_ver }}.tar.gz
cd googletest-release-${{ matrix.gtest_ver }}/googletest
cmake .
cd googletest-release-${{ matrix.gtest_ver }}
mkdir build
cd build
cmake ..
sudo make install
- name: Install Boost.Test
run: |
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.1.0

- Added support for `GTEST_SKIP()` skipped message from Google Test 1.11.

# 2.0.0

- `pytest-cpp` now supports [Catch2](https://github.com/catchorg/Catch2). Many thanks to [@salim-runsafe](https://github.com/salim-runsafe) for the contribution.
Expand Down
22 changes: 16 additions & 6 deletions src/pytest_cpp/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def run_test(self, executable, test_id, test_args=(), harness=None):
if failures:
return [GoogleTestFailure(x) for x in failures], output
elif skipped:
pytest.skip()
pytest.skip("\n".join(skipped))
else:
return None, output

Expand All @@ -121,11 +121,21 @@ def _parse_xml(self, xml_filename):
failure_elements = test_case.findall("failure")
for failure_elem in failure_elements:
failures.append(failure_elem.text)
skipped = (
test_case.attrib["status"] == "notrun"
or test_case.attrib.get("result", None) == "skipped"
)
result.append((test_suite_name + "." + test_name, failures, skipped))
skippeds = []
if test_case.attrib["result"] == "skipped":
# In gtest 1.11 a skipped message was added to
# the output file
skipped_elements = test_case.findall("skipped")
for skipped_elem in skipped_elements:
skippeds.append(skipped_elem.text)
# In gtest 1.10 the skipped message is not dump,
# so if no skipped message was found just
# append a "skipped" keyword
if not skipped_elements:
skippeds.append("Skipped")
if test_case.attrib["status"] == "notrun":
skippeds.append("Disabled")
result.append((test_suite_name + "." + test_name, failures, skippeds))

return result

Expand Down
37 changes: 20 additions & 17 deletions tests/acceptance/googletest-samples/prime_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Author: wan@google.com (Zhanyong Wan)
// Author: vladl@google.com (Vlad Losev)



// This provides interface PrimeTable that determines whether a number is a
// prime and determines a next prime number. This interface is used
// in Google Test samples demonstrating use of parameterized tests.

#ifndef GTEST_SAMPLES_PRIME_TABLES_H_
#define GTEST_SAMPLES_PRIME_TABLES_H_
#ifndef GOOGLETEST_SAMPLES_PRIME_TABLES_H_
#define GOOGLETEST_SAMPLES_PRIME_TABLES_H_

#include <algorithm>

Expand All @@ -44,7 +43,7 @@ class PrimeTable {
public:
virtual ~PrimeTable() {}

// Returns true iff n is a prime number.
// Returns true if and only if n is a prime number.
virtual bool IsPrime(int n) const = 0;

// Returns the smallest prime number greater than p; or returns -1
Expand All @@ -55,7 +54,7 @@ class PrimeTable {
// Implementation #1 calculates the primes on-the-fly.
class OnTheFlyPrimeTable : public PrimeTable {
public:
virtual bool IsPrime(int n) const {
bool IsPrime(int n) const override {
if (n <= 1) return false;

for (int i = 2; i*i <= n; i++) {
Expand All @@ -66,12 +65,12 @@ class OnTheFlyPrimeTable : public PrimeTable {
return true;
}

virtual int GetNextPrime(int p) const {
for (int n = p + 1; n > 0; n++) {
int GetNextPrime(int p) const override {
if (p < 0) return -1;

for (int n = p + 1;; n++) {
if (IsPrime(n)) return n;
}

return -1;
}
};

Expand All @@ -84,13 +83,13 @@ class PreCalculatedPrimeTable : public PrimeTable {
: is_prime_size_(max + 1), is_prime_(new bool[max + 1]) {
CalculatePrimesUpTo(max);
}
virtual ~PreCalculatedPrimeTable() { delete[] is_prime_; }
~PreCalculatedPrimeTable() override { delete[] is_prime_; }

virtual bool IsPrime(int n) const {
bool IsPrime(int n) const override {
return 0 <= n && n < is_prime_size_ && is_prime_[n];
}

virtual int GetNextPrime(int p) const {
int GetNextPrime(int p) const override {
for (int n = p + 1; n < is_prime_size_; n++) {
if (is_prime_[n]) return n;
}
Expand All @@ -103,11 +102,15 @@ class PreCalculatedPrimeTable : public PrimeTable {
::std::fill(is_prime_, is_prime_ + is_prime_size_, true);
is_prime_[0] = is_prime_[1] = false;

for (int i = 2; i <= max; i++) {
// Checks every candidate for prime number (we know that 2 is the only even
// prime).
for (int i = 2; i*i <= max; i += i%2+1) {
if (!is_prime_[i]) continue;

// Marks all multiples of i (except i itself) as non-prime.
for (int j = 2*i; j <= max; j += i) {
// We are starting here from i-th multiplier, because all smaller
// complex numbers were already marked.
for (int j = i*i; j <= max; j += i) {
is_prime_[j] = false;
}
}
Expand All @@ -120,4 +123,4 @@ class PreCalculatedPrimeTable : public PrimeTable {
void operator=(const PreCalculatedPrimeTable& rhs);
};

#endif // GTEST_SAMPLES_PRIME_TABLES_H_
#endif // GOOGLETEST_SAMPLES_PRIME_TABLES_H_
6 changes: 2 additions & 4 deletions tests/acceptance/googletest-samples/sample1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// A sample program demonstrating using Google C++ testing framework.
//
// Author: wan@google.com (Zhanyong Wan)

#include "sample1.h"

Expand All @@ -43,7 +41,7 @@ int Factorial(int n) {
return result;
}

// Returns true iff n is a prime number.
// Returns true if and only if n is a prime number.
bool IsPrime(int n) {
// Trivial case 1: small numbers
if (n <= 1) return false;
Expand All @@ -55,7 +53,7 @@ bool IsPrime(int n) {

// Try to divide n by every odd number i, starting from 3
for (int i = 3; ; i += 2) {
// We only have to try i up to the squre root of n
// We only have to try i up to the square root of n
if (i > n/i) break;

// Now, we have i <= n/i < n.
Expand Down
10 changes: 4 additions & 6 deletions tests/acceptance/googletest-samples/sample1.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// A sample program demonstrating using Google C++ testing framework.
//
// Author: wan@google.com (Zhanyong Wan)

#ifndef GTEST_SAMPLES_SAMPLE1_H_
#define GTEST_SAMPLES_SAMPLE1_H_
#ifndef GOOGLETEST_SAMPLES_SAMPLE1_H_
#define GOOGLETEST_SAMPLES_SAMPLE1_H_

// Returns n! (the factorial of n). For negative n, n! is defined to be 1.
int Factorial(int n);

// Returns true iff n is a prime number.
// Returns true if and only if n is a prime number.
bool IsPrime(int n);

#endif // GTEST_SAMPLES_SAMPLE1_H_
#endif // GOOGLETEST_SAMPLES_SAMPLE1_H_
13 changes: 4 additions & 9 deletions tests/acceptance/googletest-samples/sample10_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Author: vladl@google.com (Vlad Losev)


// This sample shows how to use Google Test listener API to implement
// a primitive leak checker.
Expand All @@ -35,18 +34,15 @@
#include <stdlib.h>

#include "gtest/gtest.h"

using ::testing::EmptyTestEventListener;
using ::testing::InitGoogleTest;
using ::testing::Test;
using ::testing::TestCase;
using ::testing::TestEventListeners;
using ::testing::TestInfo;
using ::testing::TestPartResult;
using ::testing::UnitTest;

namespace {

// We will track memory used by this class.
class Water {
public:
Expand Down Expand Up @@ -78,12 +74,12 @@ int Water::allocated_ = 0;
class LeakChecker : public EmptyTestEventListener {
private:
// Called before a test starts.
virtual void OnTestStart(const TestInfo& /* test_info */) {
void OnTestStart(const TestInfo& /* test_info */) override {
initially_allocated_ = Water::allocated();
}

// Called after a test ends.
virtual void OnTestEnd(const TestInfo& /* test_info */) {
void OnTestEnd(const TestInfo& /* test_info */) override {
int difference = Water::allocated() - initially_allocated_;

// You can generate a failure in any event handler except
Expand All @@ -104,9 +100,8 @@ TEST(ListenersTest, DoesNotLeak) {
// specified.
TEST(ListenersTest, LeaksWater) {
Water* water = new Water;
EXPECT_TRUE(water != NULL);
EXPECT_TRUE(water != nullptr);
}

} // namespace

int main(int argc, char **argv) {
Expand Down
6 changes: 2 additions & 4 deletions tests/acceptance/googletest-samples/sample1_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// A sample program demonstrating using Google C++ testing framework.
//
// Author: wan@google.com (Zhanyong Wan)


// This sample shows how to write a simple unit test for a function,
// using Google C++ testing framework.
Expand All @@ -46,7 +43,7 @@
#include <limits.h>
#include "sample1.h"
#include "gtest/gtest.h"

namespace {

// Step 2. Use the TEST macro to define your tests.
//
Expand Down Expand Up @@ -139,6 +136,7 @@ TEST(IsPrimeTest, Positive) {
EXPECT_FALSE(IsPrime(6));
EXPECT_TRUE(IsPrime(23));
}
} // namespace

// Step 3. Call RUN_ALL_TESTS() in main().
//
Expand Down
4 changes: 1 addition & 3 deletions tests/acceptance/googletest-samples/sample2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// A sample program demonstrating using Google C++ testing framework.
//
// Author: wan@google.com (Zhanyong Wan)

#include "sample2.h"

#include <string.h>

// Clones a 0-terminated C string, allocating memory using new.
const char* MyString::CloneCString(const char* a_c_string) {
if (a_c_string == NULL) return NULL;
if (a_c_string == nullptr) return nullptr;

const size_t len = strlen(a_c_string);
char* const clone = new char[ len + 1 ];
Expand Down
19 changes: 7 additions & 12 deletions tests/acceptance/googletest-samples/sample2.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// A sample program demonstrating using Google C++ testing framework.
//
// Author: wan@google.com (Zhanyong Wan)

#ifndef GTEST_SAMPLES_SAMPLE2_H_
#define GTEST_SAMPLES_SAMPLE2_H_
#ifndef GOOGLETEST_SAMPLES_SAMPLE2_H_
#define GOOGLETEST_SAMPLES_SAMPLE2_H_

#include <string.h>

Expand All @@ -52,15 +50,15 @@ class MyString {
// C'tors

// The default c'tor constructs a NULL string.
MyString() : c_string_(NULL) {}
MyString() : c_string_(nullptr) {}

// Constructs a MyString by cloning a 0-terminated C string.
explicit MyString(const char* a_c_string) : c_string_(NULL) {
explicit MyString(const char* a_c_string) : c_string_(nullptr) {
Set(a_c_string);
}

// Copy c'tor
MyString(const MyString& string) : c_string_(NULL) {
MyString(const MyString& string) : c_string_(nullptr) {
Set(string.c_string_);
}

Expand All @@ -73,13 +71,10 @@ class MyString {
// Gets the 0-terminated C string this MyString object represents.
const char* c_string() const { return c_string_; }

size_t Length() const {
return c_string_ == NULL ? 0 : strlen(c_string_);
}
size_t Length() const { return c_string_ == nullptr ? 0 : strlen(c_string_); }

// Sets the 0-terminated C string this MyString object represents.
void Set(const char* c_string);
};


#endif // GTEST_SAMPLES_SAMPLE2_H_
#endif // GOOGLETEST_SAMPLES_SAMPLE2_H_
12 changes: 5 additions & 7 deletions tests/acceptance/googletest-samples/sample2_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// A sample program demonstrating using Google C++ testing framework.
//
// Author: wan@google.com (Zhanyong Wan)


// This sample shows how to write a more complex unit test for a class
// that has multiple member functions.
Expand All @@ -42,7 +39,7 @@

#include "sample2.h"
#include "gtest/gtest.h"

namespace {
// In this example, we test the MyString class (a simple string).

// Tests the default c'tor.
Expand All @@ -69,7 +66,7 @@ TEST(MyString, DefaultConstructor) {
// we have to live with this fact.
//
// </TechnicalDetails>
EXPECT_STREQ(NULL, s.c_string());
EXPECT_STREQ(nullptr, s.c_string());

EXPECT_EQ(0u, s.Length());
}
Expand Down Expand Up @@ -104,6 +101,7 @@ TEST(MyString, Set) {
EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));

// Can we set the MyString to NULL?
s.Set(NULL);
EXPECT_STREQ(NULL, s.c_string());
s.Set(nullptr);
EXPECT_STREQ(nullptr, s.c_string());
}
} // namespace

0 comments on commit 2a2f125

Please sign in to comment.