0% found this document useful (0 votes)
47 views45 pages

方俊贤 (Ken Fang) 首席咨询顾问, 雅各布森软件(北京)有限公司 微信号: Kenfang - Lisp

敏捷自动化单元测试

Uploaded by

gameOver
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views45 pages

方俊贤 (Ken Fang) 首席咨询顾问, 雅各布森软件(北京)有限公司 微信号: Kenfang - Lisp

敏捷自动化单元测试

Uploaded by

gameOver
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

(Ken Fang)

,
: KenFang_Lisp

Copyright 2013 Ivar Jacobson International SA. All rights reserved


JavaScript
JUnit
JUnit
JavaScript

QUNIT
QUnit: http://qunitjs.com/

Qunit lib :
qunit-1.15.0.css
qunit-1.15.0.js
QUnit test runner page: BasicRunner.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>QUnit test runner</title>
<link rel="stylesheet" href="lib/qunit-1.15.0.css>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="lib/qunit-1.15.0.js"></script>
</body>
</html>
QUnit Test
module("Investment", {
setup: function() {
stock = new Stock();
investment = new Investment({
stock: stock, ( User Story)
shares: 100,
sharePrice: 20
});
}, teardown: function() {
delete stock;
delete investment;
} User Story (
});

test("should be of a stock", function(){


equal(investment.stock, stock, "Invested must be a stock");
});

test("should have the invested share quantity", function() {


ok(investment.shares == 200, "Invested share quantity must equal
100");
});
QUnit Assert: http://api.qunitjs.com/category/assert/

equal()
A non-strict comparison, roughly equivalent to JUnits assertEquals.
notEqual()
A non-strict comparison, checking for inequality.
ok()
A boolean check, equivalent to CommonJSs assert.ok() and JUnits
assertTrue(). Passes if the first argument is truthy.
JavaScript QUnit test runner page : BasicRunner.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>QUnit test runner</title>
<link rel="stylesheet" href="lib/qunit-1.15.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="lib/qunit-1.15.0.js"></script>
<script src="src/Stock.js"></script>
<script src="src/Investment.js"></script>
<script src="test/InvestmentTest.js"></script>
</body>
</html>

JavaScript
JS-TEST-DRIVER (JSTD)
js-test-driver (JSTD)
JavaScript , :
TestRunner

js-test-driver, :
js-test-driver JavaScript
js-test-driver JUnit ,
JavaScript ; ,
HTML
: JavaScript , , js-
test-driver
js-test-driver (JSTD)

https://code.google.com/p/js-test-driver/

Server: Test Runner


Server: java -jar JsTestDriver.jar --port 9876
, Server
http://localhost:9876/capture
JavaScript ; , HTML
Source Folder
Test Folder
Configuration File
Configuration File: JSTD JavaScipt
Assertions
Assertion Description

assertTrue([msg], actual) Fails if the result isn't truthy

assertFalse([msg], actual) Fails if the result isn't falsy.

assertEquals([msg], expected, actual) Fails if the expected and actual values can not be
compared to be equal.
assertNotEquals([msg], expected, actual) Fails if the expected and actual values can be
compared to be equal.
assertSame([msg], expected, actual) Fails if the expected and actual values are not
references to the same object.
assertNotSame([msg], expected, actual) Fails if the expected and actual are references to
the same object
assertNull([msg], actual) Fails if the given value is not exactly null.

assertNotNull([msg], actual) Fails if the given value is exactly null.

https://code.google.com/p/js-test-driver/wiki/Assertions
Test Case
Inline declaration
TestCase("MyTestCase", {
testA:function(){
},
testB:function(){
}
});

Prototype
MyTestCase = TestCase("MyTestCase");

MyTestCase.prototype.testA = function(){
};

MyTestCase.prototype.testB = function(){
};

,
setUp()

tearDown()
Test Case
TestCase("strftimeTest", {
setUp: function () {
this.date = new Date(2014, 9, 12, 20, 10, 40);
},

tearDown: function () {
delete this.date;
},

"test %Y should reurn full year" : function () {


var year = Date.formats.Y(this.date);
assertNumber(year);
assertEquals(2009, year);
}

});
Configuration File (jsTestDriver.conf)

server: http://localhost:9876

load:
- src/*.js
- test/*.js

https://code.google.com/p/js-test-driver/wiki/ConfigurationFile
Starting the server & Capturing browsers

java -jar JsTestDriver.jar --port 9876 --browser firefoxpath,chromepath

https://code.google.com/p/js-test-driver/wiki/CommandLineFlags

java -jar JsTestDriver.jar --tests all

Total 2 tests (Passed: 2; Fails: 0; Errors: 0) (0.00 ms)

Safari 528.16: Run 1 tests (Passed: 1; Fails: 0; Errors 0) (0.00 ms)


Firefox 1.9.0.10: Run 1 tests (Passed: 1; Fails: 0; Errors 0) (0.00 ms)

Safari Firefox

Eclipse
https://code.google.com/p/js-test-driver/wiki/UsingTheEclipsePlugin
Intellij
https://code.google.com/p/js-test-driver/wiki/IntelliJPlugin

Test-Driven JavaScript Development; Christian Johansen


https://code.google.com/p/js-test-driver/
JUint
: JUnit

JUnit , :

JUnit

inputs
Test Class SUT
direct

outputs

:
@Test
public, JUnit
void ,

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class ExampleTest {


@Test
public void thisIsATestMethod() {
assertEquals(5, 2 + 3);
}
@Test
public void thisIsAnotherTestMethod() {
WorldOrder newWorldOrder = new WorldOrder();
assertFalse(newWorldOrder.isComing());
}
}
JUnit
JUnit :
Step 1) JUnit ; public class ExampleTest;
Step 2) JUnit @Before , :; DefaultController( );
; SampleRequest()
Step 3) JUnit ;
public void testProcessRequest() public void testAddHandler()
, Junit ,
Step 4) JUnit @After
, JUnit ()
public class ExampleTest {
@Before
public void setLocaleForTests() { inputs
controller = new DefaultController();
request = new SampleRequest();
Test Class SUT
handler = new SampleHandler();

direct
}
@Test
ExampleTest DefaultController
public void testProcessRequest() {
Response response = controller.processRequest( request );
assertNotNull( "Must not return a null response", response );
}
@Test
public void testAddHandler() { outputs
RequestHandler handler2 = controller.getHandler( request );
assertSame( "Controller.getHandler must return the samplehandler", handler2, handler );
}
@After
}

JUnit assertions Description


assertEquals Asserts that two objects (or primitives)
are equal
assertArrayEquals Asserts that two arrays have the same
items
assertTrue Asserts that a statement is true
assertFalse Asserts that a statement is false
assertNull Asserts that an object reference is null
assertNotNull Asserts that an object reference is not
null
assertSame Asserts that two object references point
to the same instance
JUnit
JUnit

,
, , ,

() , ,
, ,

JUnit Mockito, ,
,

User Story
: ;
: ; SUT DOC
: ; SUT DOC (Test Spy)
: ; SUT DOC
: ;
: SUT;
: ;
: ;
: ;
: SUT;
: ;
: ;
User Story:

User Story
, User Story

,

TEST SPY
inputs outputs
Test Class SUT MOCK

Indirect
direct

DOCs
Stub/ Dummy
outputs inputs MOCK
: ;

public class DailyNewsServiceTest {


@Test
public void SubscriberShouldReceiveNews() {
/* Create SUT */
DailyNewsService dailynews = new DailyNewsService();

/* Create DOCs */
Subscriber subscriber = mock(Subscriber.class);
News news = mock(News.class);
}
}

TEST SPY
inputs outputs [Subscriber]
Test Class SUT MOCK

Indirect
[DailyNewsService]
direct

[DailyNewsServiceTes]

DOCs
Dummy
outputs [News]
inputs MOCK
: ; SUT DOC

/* SUT; DailyNewsService; DailyNewsService */


public class DailyNewsService {
}

/* DOC; Subscriber, News; Subscriber, News */


public interface Subscriber {
}

public interface News {


}

inputs outputs TEST SPY[Subscriber]


Test Class SUT MOCK

Indirect
direct

[DailyNewsServiceTes] [DailyNewsService]

DOCs
Dummy
outputs [News]
inputs MOCK
: ; SUT DOC (Test Spy)
public class DailyNewsServiceTest {
@Test
public void SubscriberShouldReceiveNews() {
/* Create SUT */
DailyNewsService dailynews = new DailyNewsService();

/* Create DOCs */
Subscriber subscriber = mock(Subscriber.class);
News news = mock(News.class);

/* DOC SUT */
dailynews.addSubscriber(subscriber);

/* SUT */
dailynews.send(news);

/* ; SUT DOC (Test Spy) */


verify(subscriber).receive(news);
}
}

inputs outputsTEST SPY


Test Class SUT MOCK [Subscriber]
Indirect
direct

[DailyNewsServiceTes] [DailyNewsService]
DOCs
Dummy[News]
outputs inputs MOCK
: ; SUT DOC

/* SUT: DailyNewsService */
public class DailyNewsService {

public void addSubscriber(Subscriber subscriber) {


}

public void send(News news) {


}
}

/* DOC (test spies); Subscriber */


public interface Subscriber {
void receive (News news);
}

inputs outputs TEST SPY[Subscriber]


Test Class SUT MOCK

Indirect
direct

[DailyNewsServiceTes] [DailyNewsService]

DOCs
Dummy
outputs [News]
inputs MOCK
: ;

SUT (DailyNewsService) DOC (Subscriber)

TEST SPY
inputs outputs
[Subscriber]
Test Class SUT MOCK

Indirect
[DailyNewsService]
direct

[DailyNewsServiceTes]

DOCs
Dummy
[News]
outputs inputs MOCK
: SUT;

public class DailyNewsService {


private Subscriber subscriber;

public void addSubscriber(Subscriber subscriber) {


this.subscriber = subscriber;
}

public void send(News news) {


subscriber.receive(news);
}
}

SUT (DailyNewsService) DOC (Subscriber)

inputs outputs TEST SPY[Subscriber]


Test Class SUT MOCK

Indirect
direct

[DailyNewsServiceTes] [DailyNewsService]
DOCs
Dummy
outputs [News]
inputs MOCK
: ;
: ;

public class DailyNewsServiceTest {


@Test
..

@Test
public void newsShouldBeSentToAllSubscribers() {
/* Create SUT */
DailyNewsService dailynews = new DailyNewsService();

/* Create DOCs */
Subscriber subscriberA = mock(Subscriber.class, "subscriberA");
Subscriber subscriberB = mock(Subscriber.class, "subscriberB");
News news = mock(News.class);

/* DOC SUT */
dailynews.addSubscriber(subscriberA);
dailynews.addSubscriber(subscriberB);

/* SUT */
dailynews.send(news);

/* ; SUT DOC (Test Spy) */


verify(subscriberA).receive(news);
verify(subscriberB).receive(news);
}
}
: ;

SUT (DailyNewsService) DOC (Subscriber A)

TEST SPY
MOCK [Subscriber A]

TEST SPY
inputs outputs
[Subscriber B]
Test Class SUT MOCK

Indirect
[DailyNewsService]
direct

[DailyNewsServiceTes]

DOCs
Dummy
[News]
outputs inputs MOCK
: SUT;

public class DailyNewsService {


private Collection<Subscriber> subscribers = new ArrayList<Subscriber>();

public void addSubscriber(Subscriber subscriber) {


subscribers.add(subscriber);
}

public void send(News news) {


for (Subscriber subscriber : subscribers) {
subscriber.receive(news);
}
}
}

TEST SPY
MOCK [Subscriber A]

inputs outputs TEST SPY


Test Class SUT [Subscriber B]

Indirect
MOCK
direct

[DailyNewsService]
DOCs
Dummy
outputs inputs [News]
MOCK
: ;
: ;
public class DailyNewsServiceTest {
.
@Test
public void notSubscribedShouldNotReceiveNews() {

/* Create SUT */
DailyNewsService dailynews = new DailyNewsService();

/* Create DOCs */
Subscriber subscriberX = mock(Subscriber.class, "subscriberX");
Subscriber subscriberY = mock(Subscriber.class, "subscriberY");
News news = mock(News.class);

/* SUT */
dailynews.send(news);

/* ; SUT DOC (Test Spy) ; subscriberX , subscriberY */


verify(subscriberX, never()).receive(news);
verify(subscriberY, never()).receive(news);
}
}

Mockito never(); ,

JavaScript
JUint

inputs
Test Class SUT
direct

outputs
;

DailyNewsService (SUT) Subscriber, News (DOCs)


Subscriber, News (DOCs) ,

DailyNewsService (SUT)
Subscriber, News (DOCs)
, , ,

, , , , ,
, ,
TEST SPY
MOCK [Subscriber A]

inputs outputs TEST SPY


Test Class SUT [Subscriber B]

Indirect
MOCK
direct

[DailyNewsService]
DOCs
Dummy
outputs inputs [News]
MOCK

You might also like