8000 Add DSL for setup/teardown of entire test suite by sharplet · Pull Request #427 · kiwi-bdd/Kiwi · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add DSL for setup/teardown of entire test suite #427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 7, 2014
Merged

Add DSL for setup/teardown of entire test suite #427

merged 4 commits into from
Feb 7, 2014

Conversation

sharplet
Copy link
Contributor

This should hopefully address https://github.com/allending/Kiwi/issues/266. Works for both OCUnit and XCTest.

Example usage:

#import <Kiwi/Kiwi.h>

CONFIG_START

beforeAllSpecs(^{
    NSLog(@"BEFORE ALL SPECS");
});

afterAllSpecs(^{
    NSLog(@"AFTER ALL SPECS");
});

CONFIG_END

(Add this code to a file <tests-dir>/Support/Config.m and ensure it is part of your unit test bundle's Compile Sources build phase.)

I'd love to go further and add support for beforeEachSpec, afterEachSpec, beforeEachExample and afterEachExample, however it would be great to get feedback on this API first.

Adam Sharp added 4 commits December 17, 2013 08:09
This is the bare bones required to hook into setup and teardown of an
entire SenTestSuite. This is a starting point that could make way for a
real DSL for Kiwi test suite configuration.
Example usage:

    #import <Kiwi/Kiwi.h>

    CONFIG_START

    beforeAllSpecs(^{
        NSLog(@"BEFORE ALL SPECS");
    });

    afterAllSpecs(^{
        NSLog(@"AFTER ALL SPECS");
    });

    CONFIG_END

(Add this code to a file `<tests-dir>/Support/Config.m` and ensure it is
part of your unit test bundle's Compile Sources build phase.)

- - -

Defines a `KWSuiteConfigurationBase` class that provides a
`+defaultConfiguration` singleton instance. The first time this method
is called, we look for a class named `KWSuiteConfiguration` that is a
subclass of `KWSuiteConfigurationBase`, and instantiate it. This class
is declared by using the `CONFIG_START` and `CONFIG_END` macros.

When the default configuration receives the `-setUp` message, it sends
`-configureSuite` to `self`, which executes the code between
`CONFIG_START` and `CONFIG_END`.

The blocks passed to `beforeAllSpecs` and `afterAllSpecs` are then
executed in `-setUp` and `-tearDown`, respectively.
Extracts `+[SenTestSuite patchTestSuiteForBundlePathIMP]` into its own
category defined in TestSuiteConfigurationAdditions.h, extending either
`SenTestSuite` or `XCTestSuite`, depending on whether Kiwi is compiled
with the `XCT_EXPORT` macro.
Removes duplication of `#ifdef XCT_EXPORT ... #endif` directives by
instead providing macros that expand to the appropriate class name
(e.g., `SenTestSuite` vs. `XCTestSuite`, `SenTestCase` vs.
`XCTestCase`).
@sharplet
Copy link
Contributor Author

Here's a working example: sharplet/EnumeratorKit@0cd122b.

@sharplet
Copy link
Contributor Author

Any update on this? Would be great to get some feedback.

@NachoSoto
Copy link
Contributor

Yeah this would be great! The implementation looks good.

@supermarin
Copy link

@sharplet I've originally had a different idea of implementing this, but if it's stable enough we can merge it.

Would you mind adding a functional test for it?

supermarin added a commit that referenced this pull request Feb 7, 2014
Add DSL for setup/teardown of entire test suite
@supermarin supermarin merged commit af20492 into kiwi-bdd:master Feb 7, 2014
@supermarin
Copy link

@sharplet thank you very much sir, merging so everybody can use the feature.

Still, if you have time, some tests would be appreciated.

@modocache
Copy link
Member

@sharplet This is an amazing feature. Mad props!

@sharplet sharplet deleted the config-hooks branch February 8, 2014 05:33
@sharplet
Copy link
Contributor Author
sharplet commented Feb 8, 2014

Thanks guys! 😃

Re. adding a functional test for this: I've been thinking about how I would go about testing it, and am not really sure how to proceed. For example, with the afterAllSpecs block: how, from within the tests, do you execute something that's meant to be run after all the tests are complete? So what I was thinking is that I would need to basically need to perform a minimal, controlled test run in a child process, that logs something to stdout in beforeAllSpecs and afterAllSpecs, and then test that the output was as expected. How does that sound?

Let me know if you have any other (simpler) ideas.

Also, I tried this out on one of our projects at work and unfortunately the hooks weren't executed. I haven't yet figured out what the difference was, but perhaps there are some test bundle configurations where testSuiteForBundlePath: is never executed. If that's the case, we may need to find some other way to get at the top-level test suite object before the tests run.

@supermarin
Copy link

@sharplet right - that's what my concern was.

I've had in mind subclassing XCTestLog or XCTestObserver and using something like

+ (void)initialize {
    [[NSUserDefaults standardUserDefaults] setValue:@"VATestObserver"
                                             forKey:XCTestObserverClassKey];
    [super initialize];
}

More info:
http://stackoverflow.com/questions/19136767/generate-gcda-files-with-xcode5-ios7-simulator-and-xctest
http://www.bubblefoundry.com/blog/2013/11/code-coverage-revisited/

@fcy
< 8000 svg aria-label="Show options" role="img" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-kebab-horizontal"> Copy link
fcy commented Feb 28, 2014

This implementation doesn't work if the test target is an application test, that is has a bundle loader and the Target in the General tab is an app. Both beforeAllSpecs and afterAllSpecs don't get called.

I've created a sample project here based on the original EnumerationKit example: https://dl.dropboxusercontent.com/u/2702446/EnumeratorKit.zip

What I did was create an application target called App and changed the Specs target to use App as its target.

@sharplet
Copy link
Contributor Author

Thanks for the report @fcy — I started work on a fix for application tests a little while ago, and just haven't had time to tidy it up and push it yet.

@supermarin For XCTest, I think an XCTestObserver is the right way to go, as it defines the -startObserving and -stopObserving hooks. SenTestingKit doesn't have those unfortunately; however, for application tests +defaultTestSuite is called, rather than +testSuiteForBundlePath: — we should be able to just swizzle both of these methods.

@fcy
Copy link
fcy commented Feb 28, 2014

Good to know 👍

Humm looks I could create a XCTestObserver while this feature is not available.

@supermarin
Copy link

@sharplet I'm all down for it if implementation will be simple and XCTest only.
People should switch, it's not that hard.

@fcy
Copy link
fcy commented Feb 28, 2014

👍 for supporting only XCTest

FYI, I've created an XCTestObserver it is pretty easy and straightforward:

@implementation TTTestObserver

- (void)startObserving {
    [super startObserving];
    JSObjectionInjector *objectionInjector = [JSObjection defaultInjector];
    [JSObjection setDefaultInjector:[objectionInjector withModule:[[SpecsJsonObjectionModule alloc] init]]];
}

@end

This was referenced Mar 8, 2014
modocache added a commit to modocache/Kiwi that referenced this pull request Mar 11, 2014
Reverts c811d87, 090a4ab, 253322a, and e86cc03, part of pull
request kiwi-bdd#427, in favor of resolving issue kiwi-bdd#473.

This removes the `CONFIG_START` and `CONFIG_END` macros in the hopes of
adding them again, after further testing, at a later date.
modocache added a commit to modocache/Kiwi that referenced this pull request Mar 11, 2014
Reverts c811d87, 090a4ab, 253322a, and e86cc03, part of pull
request kiwi-bdd#427, in favor of resolving issue kiwi-bdd#473.

This removes the `CONFIG_START` and `CONFIG_END` macros in the hopes of
adding them again, after further testing, at a later date.
modocache added a commit that referenced this pull request Mar 13, 2014
@darend
Copy link
darend commented Jul 28, 2014

Was restored in 2.3.0 1a6a5e6

@huynguyen
Copy link

@sharplet I'm interested in trying to get the beforeEachExample working. Do you have any pointers to pick up where you left off with it?

@sharplet
Copy link
Contributor Author

@huynguyen That's great!

I'm thinking it should be possible to implement the -setUp and -tearDown methods on KWSpec and then hook into [KWSuiteConfiguration defaultConfiguration] from there... It would be pretty fantastic if it was as simple as that, and hopefully there wouldn't need to be any swizzling involved.

Does that make sense?

@huynguyen
Copy link

@sharplet yea, I'll give it a shot and open a PR when I got something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants
0