Giter VIP home page Giter VIP logo

gmock-xcode's Introduction

GoogleMock - Xcode

Build Status Issues Status License
Contact Sponsor

About

This project consists of an integration of the GoogleMock library to Apple's Xcode IDE.

Since version 5, Xcode provides a nice way to write unit tests with the XCTest framework.
Unit tests written with XCTest are integrated to the IDE, providing visual feedback when running tests. They are also integrated with the different build toolchains, like xcodebuild or Facebook's xctool.

It was specifially designed for Objective-C, writing unit tests for other languages (such as C++) is not so great with XCTest. Of course, this is possible using Objective-C++, but writing an Objective-C class for each C++ test case leads to some undesired overhead.

GoogleMock is nice unit testing library for C++. The only issue is that it does not integrate well with Xcode, as does XCTest.
This mean the IDE won't provide visual feedback for the unit tests written with GoogleMock, and you'll have to look at the console output to inspect any failed test.

So this project fixes this issue by bridging GoogleMock to XCTest.

Implementation details

XCTest works by analysing test classes at runtime, thanks to Objective-C dynamic nature.
Basically, when the test bundle is loaded, it will look for all classes extending XCTestCase, and look for methods beginning with the test prefix.

If such a class is available, it will then create an instance, and launch each test method, reporting the result to the IDE.

This project consists of a framework which has to be linked to the unit test bundle. The framework includes GoogleMock, so you don't have to build it by yourself.

It works by querying each GoogleMock test case. For each one, a specific subclass of XCTestCase will be dynamically created at runtime.
For each test in a test case, an Objective-C method will be created and added to the XCTestCase subclass.

The GoogleMock test cases are then run, reporting the usual output to the console.

Then, as we created new classes compatible with XCTest, it will find them, and run them as usual.
Each method will simply look at the GoogleMock test result, and report the status using XCTest assertions, so we got visual feedback, as if we had written our test cases using XCTest.

Project Configuration

In order to use these features, your unit test bundle needs to be linked to the provided frameworks.

The easiest way to do this is to include the provided Xcode project (GoogleMock.xcodeproj) as a subproject of your own Xcode project, so all targets are available.

Then, from the Build Phases screen of your unit test's target, add the following frameworks to the Target Dependancies step:

  • GoogleMock

Adds the same frameworks to the Link Binary With Libraries step, and you're done.
Your unit test bundle is now ready to use GoogleMock.

Writing tests

Writing GoogleMock tests is also straightforward.
Simply include <GoogleMock/GoogleMock.h> to your .cpp file, and write your tests as usual.

For instance:

#include <GoogleMock/GoogleMock.h>

using namespace testing;

TEST( MyTestCase, MyTest )
{
    ASSERT_TRUE( true );
}

In the above example, a subclass of XCTestCase named MyTestCase will be created at runtime, with a test method called testMyTest, so you can easily inspect the results of your GoogleMock tests directly from the Xcode tests tab.

License

The GoogleMock Xcode integration library is released under the terms of the MIT License.

Repository Infos

Owner:			Jean-David Gadina - XS-Labs
Web:			www.xs-labs.com
Blog:			www.noxeos.com
Twitter:		@macmade
GitHub:			github.com/macmade
LinkedIn:		ch.linkedin.com/in/macmade/
StackOverflow:	stackoverflow.com/users/182676/macmade

gmock-xcode's People

Contributors

alekseyzakharo avatar macmade avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

gmock-xcode's Issues

testHasGoogleMockTests always failing.

I created a new test target from test navigator and selected language as objective-C as that is the only option available. I am using XCode 6. I added GoogleMock, gtest and gmock to "Target Dependencies" and "Link Binary With Libraries" in "Build Phases" setting. I add a .cpp file which contains the sample test code as written in README of gmock-xcode. I added that .cpp file in "Compile Sources" as well in "Build Phases" settings.

But when I run the test target from test navigator, it compiles and links properly, but testHasGoogleMockTests always fails as it is not able to detect the sample tests added in .cpp file.

What I am missing?

Test bridge crashes after running tests

Hi. Great work with gmock-xcode!

I am experiencing a crash with a basic "hello world" test project...

Steps to reproduce:

  • Create a new empty Xcode project
  • Add a macOS Unit Testing Bundle Target
  • Add GoogleMock.xcodeproj as a subproject
  • Copy Test.cpp to the new project and add it to the testing target
  • Add GoogleMock to the Target Dependencies and Link With Libraries in Build Phases
  • Product -> Test

Behaviour:

  • The test runs successfully and passes
  • The test bridge crashes in GMXCTestBridge.mm:267 with error:

Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)

Call stack:

Thread 1 Queue : com.apple.main-thread (serial)
#0	0x00007fff5e34b682 in _mapStrHash(_NXMapTable*, void const*) ()
#1	0x00007fff5e34b5c7 in _NXMapMember(_NXMapTable*, void const*, void**) ()
#2	0x00007fff5e34b58d in NXMapGet ()
#3	0x00007fff5e35143d in getClass_impl(char const*) ()
#4	0x00007fff5e3513ed in getClass(char const*) ()
#5	0x00007fff5e3570af in objc_allocateClassPair ()
#6	0x0000000106b891b4 in ::+[GoogleMockXCTestBridge initialize]() at /Users/jamie/Documents/Noiiz/git/noiiz-plugin-module/Tests/External/gmock-xcode/GoogleMock/Classes/GMXCTestBridge.mm:267
#7	0x00007fff5e35e2de in CALLING_SOME_+initialize_METHOD ()
#8	0x00007fff5e34f3c9 in _class_initialize ()
#9	0x00007fff5e34ee8f in lookUpImpOrForward ()
#10	0x00007fff5e34e914 in _objc_msgSend_uncached ()
#11	0x00007fff370f9a20 in -[__NSSetM addObject:] ()
#12	0x00000001003d0270 in +[XCTestCase(RuntimeUtilities) allSubclasses] ()
#13	0x000000010036714d in +[XCTestSuite _suiteForBundleCache] ()
#14	0x00000001003694e4 in -[XCTestSuite _initWithTestConfiguration:] ()
#15	0x000000010036a2c8 in +[XCTestSuite testSuiteForTestConfiguration:] ()
#16	0x00000001003e606f in -[XCTTestRunSession runTestsAndReturnError:] ()
#17	0x000000010034f865 in -[XCTestDriver runTestsAndReturnError:] ()
#18	0x00000001003d10f3 in _XCTestMain ()
#19	0x0000000100002155 in main ()
#20	0x00007fff5ef75015 in start ()
#21	0x00007fff5ef75015 in start ()

How to include test files from other directory?

This is not specifically any issue, rather clarification needed to use it. My requirement is that I want to keep all test.cpp files in some other directory outside gmock-xcode, not in GoogleMockTests directory. If I add a directory in GoogleMockTests from xcode UI, it does not run the tests in the directory using xcode ui to run the tests.

"Better schemes - Coverage in Xcode" breaks my tests.

For some reason that commit results in a build that fails to find/run any of my tests. Commit right before works fine.

Did this cause anything to change as to how the tests need to be set up for them to be found?

testHasGoogleMockTests fires when running the tests.

First off, thanks for creating this XCTest bridge project! It is very useful.

I must be doing something wrong, I followed the instructions in the README and everything compiles and links fine. However, when I run my test from Xcode (from Test Navigator) I get this assert firing in GMXCTestBridge.mm:

  • ( void )testHasGoogleMockTests
    {
    XCTAssertTrue( __inited, "No GMock test" );
    }

Could not find anything on the Internet related to this. Seems that my tests are not registering? Can you please update the instructions so this doesn't happen anymore?

Thanks!

Florin

Seeing following error

Hello I tried to link gmock , gtest and GoogleMock from gmock-xcode repo to write some Mock test cases. I was able to link all three at runtime to my test target However it complains with following error message "ld: framework not found XCTest" . I tried linking it from following path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.Platform/Developer/Library/Frameworks/XCTest.framework but it still complains with same error message. Is this a known issue

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.