Skip to content

Instantly share code, notes, and snippets.

@rudyjahchan
Created July 18, 2011 22:45
Show Gist options
  • Save rudyjahchan/1090887 to your computer and use it in GitHub Desktop.
Save rudyjahchan/1090887 to your computer and use it in GitHub Desktop.
Samples of Test Code in various frameworks
Feature: Demonstrating the touchx:y: api
Background:
Given I launch the app
Scenario: Add test user
# about where "Larry Stooge" row should be in main screen
When I touch the screen at (80,90)
Then I wait to see a navigation bar titled "User Profile"
And I should see "Larry"
And I should see "Stooge"
And I should see "larry@stooges.com"
Feature: Carbon Five Google Apps Account Authentication
In order to track time
As a user
I can login via a Carbon Five Google Apps account
Scenario: Sign Up
When I sign up with the following Google Apps account:
| name | email |
| Example User | example@carbonfive.com |
Then the following employee should exist:
| name | email |
| Example User | example@carbonfive.com |
And I should be logged in
Scenario: Sign In
Given the following employee exists:
| name | email |
| Example User | example@carbonfive.com |
When I login as "example@carbonfive.com"
Then I should be logged in
And the following employees should exist:
| name | email |
| Example User | example@carbonfive.com |
And I should be on the tasks page
Feature: iPhone integration tests
In order to test my iphone application
As a developer
I want cucumber to be able to drive the simulator
Background:
Given "app/Universal.xcodeproj" is loaded in the iphone simulator
Scenario: Scrolling
When I tap "Show Test Modal"
Then I should see "Lorem ipsum dolor"
But I should not see "Fusce sem nisi"
When I scroll down to "Fusce sem nisi"
Then I should see "Fusce sem nisi"
But I should not see "Lorem ipsum dolor"
require 'spec_helper'
describe MyCounter do
before do
@counter = MyCounter.init
end
describe '#init' do
context 'when a counter is created' do
it 'starts the count at 0' do
@counter.count.should == 0
end
end
end
describe '#increment' do
context 'given a count less than 100' do
before do
@old_count = @counter.count
@counter.increment
end
it 'increases the count by 1' do
@counter.count.should == @old_count + 1
end
end
context 'given a count of 100' do
before do
@counter.count = 100
@counter.increment
end
it 'resets the counter to 0' do
@counter.count.should == 0
end
end
end
end
-(void) testInit {
assertThat(_counter, is(notNilValue()));
assertThatInt(_counter.count, is(equalToInt()));
}
SPEC_BEGIN(MyCounterSpec)
__block MyCounter* _counter;
__block int count;
beforeEach(^{
_counter = [[MyCounter alloc] init];
});
describe(@"-init", ^{
it(@"starts the count at 0", ^{
[theValue(_counter.count) should] equal:theValue(0)];
});
});
describe(@"-increment", ^{
context(@"given the current count less than 100", ^{
beforeEach(^{
[_counter increment];
});
it(@"increments the count by 1", ^{
[theValue(_counter.count) should] equal:theValue(1)];
});
});
});
SPEC_END
-(void) testIncrementResetsToZeroAfterHundred {
id counterMock = [OCMockObject partialMockForObject:_counter];
[counterMock stub] count] andReturn:100];
[counterMock expect] reset];
[counterMock increment];
[counterMock verify];
}
SPEC_BEGIN(MyCounterSpec)
__block MyCounter* _counter;
beforeEach(^{
_counter = [[MyCounter alloc] init];
});
describe(@"-init", ^{
it(@"starts the count at 0", ^{
assertThatInt(_counter.count, is(equalToInt(0)));
});
});
describe(@"-increment", ^{
context(@"given the current count less than 100", ^{
beforeEach(^{
[_counter increment];
});
it(@"increments the count by 1", ^{
assertThatInt(_counter.count, is(equalToInt(1)));
});
});
context(@"given the current count at than 100", ^{
beforeEach(^{
for(int i = 0; i < 100; i++) {
[_counter increment];
}
});
it(@"resets the count to 0", ^{
assertThatInt(_counter.count, is(equalToInt(0)));
});
});
});
SPEC_END
@interface MyCounterTest {
@private
MyCounter* _counter;
}
-(void) testInit;
-(void) testIncrement;
-(void) testIncrementResetsToZeroAfterHundred;
-(void) testReset;
@end
@implementation MyCounterTest
-(void) setUp {
[super setup];
_counter = [[MyCounter alloc] init];
}
-(void) testInit {
STAssertNotNil(_counter, @"Could not create a counter.");
STAssertEquals(_counter.count, @"Initial count is zero.");
}
-(void) testIncrement {
[_counter increment];
STAssertEquals(counter.count, 1, @"Did not increment count.");
}
-(void) testIncrementResetsToZeroAfterHundred {
for(int i = 0; i < 100; i++) {
[_counter increment];
}
STAssertEquals(counter.count, 100, @"Did not increment count to 100.");
[_counter increment];
STAssertEquals(counter.count, 0, @"Did not reset count after 100.");
}
-(void) testReset {
[_counter increment];
[_counter increment];
STAssertEquals(counter.count, 2, @"Did not increment count.");
[_counter reset];
STAssertEquals(counter.count, 1, @"Did not increment count.");
}
-(void) tearDown {
[_counter release];
[super tearDown];
}
@end
@implementation KIFTestScenario (EXAdditions)
+ (id)scenarioToLogIn;
{
KIFTestScenario *scenario = [KIFTestScenario scenarioWithDescription:@"Test that a user can successfully log in."];
[scenario addStep:[KIFTestStep stepToReset]];
[scenario addStepsFromArray:[KIFTestStep stepsToGoToLoginPage]];
[scenario addStep:[KIFTestStep stepToEnterText:@"user@example.com" intoViewWithAccessibilityLabel:@"Login User Name"]];
[scenario addStep:[KIFTestStep stepToEnterText:@"thisismypassword" intoViewWithAccessibilityLabel:@"Login Password"]];
[scenario addStep:[KIFTestStep stepToTapViewWithAccessibilityLabel:@"Log In"]];
// Verify that the login succeeded
[scenario addStep:[KIFTestStep stepToWaitForTappableViewWithAccessibilityLabel:@"Welcome"]];
return scenario;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment