被单元测试的类
![程序代码](http://www.wolf2999.cn/images/code.gif)
package
{
public class BankAccount
{
private var balance:Number=0;
public function deposit(amount:Number):void{
balance=balance+amount;
}
public function withdraw(amount:Number):void{
balance=balance-amount;
}
public function getBalance():Number{
return balance;
}
}
}
{
public class BankAccount
{
private var balance:Number=0;
public function deposit(amount:Number):void{
balance=balance+amount;
}
public function withdraw(amount:Number):void{
balance=balance-amount;
}
public function getBalance():Number{
return balance;
}
}
}
单元测试类:
![程序代码](http://www.wolf2999.cn/images/code.gif)
package {
import asunit.framework.TestCase;
public class BankAccountTest extends TestCase {
private var instance:BankAccount;
/**
* Constructor
*/
public function BankAccountTest(testMethod:String) {
super(testMethod);
}
/**
* Set up instance of BankAccount to test with
*/
protected override function setUp():void {
instance = new BankAccount();
}
/**
* Delete instance used for testing
*/
protected override function tearDown():void {
instance=null;
}
/**
* Test BankAccount instantiation
*/
public function testInstantiated():void {
assertTrue("Example instantiated", instance is BankAccount);
}
/**
* Test Deposit
*/
public function testDeposit():void {
instance.deposit(50);
assertTrue("Balance on a new account after 50 deposit is 50", instance.getBalance() == 50);
instance.deposit(25);
assertEquals("Balance after 50 deposit and another 25 deposit is 75", 75,instance.getBalance());
}
/**
* Test withdraw
*/
public function testWithdraw():void {
instance.deposit(100);
instance.withdraw(50);
assertTrue("Balance on a new account after 100 deposit and a 50 withdraw is 50", instance.getBalance() == 50);
}
}
}
import asunit.framework.TestCase;
public class BankAccountTest extends TestCase {
private var instance:BankAccount;
/**
* Constructor
*/
public function BankAccountTest(testMethod:String) {
super(testMethod);
}
/**
* Set up instance of BankAccount to test with
*/
protected override function setUp():void {
instance = new BankAccount();
}
/**
* Delete instance used for testing
*/
protected override function tearDown():void {
instance=null;
}
/**
* Test BankAccount instantiation
*/
public function testInstantiated():void {
assertTrue("Example instantiated", instance is BankAccount);
}
/**
* Test Deposit
*/
public function testDeposit():void {
instance.deposit(50);
assertTrue("Balance on a new account after 50 deposit is 50", instance.getBalance() == 50);
instance.deposit(25);
assertEquals("Balance after 50 deposit and another 25 deposit is 75", 75,instance.getBalance());
}
/**
* Test withdraw
*/
public function testWithdraw():void {
instance.deposit(100);
instance.withdraw(50);
assertTrue("Balance on a new account after 100 deposit and a 50 withdraw is 50", instance.getBalance() == 50);
}
}
}
Alltest.as 集中所有测试类:
![程序代码](http://www.wolf2999.cn/images/code.gif)
package {
import asunit.framework.TestSuite;
import flash.display.Sprite;
public class AllTests extends TestSuite {
public function AllTests() {
super();
addTest(new BankAccountTest("testInstantiated"));
addTest(new BankAccountTest("testDeposit"));
addTest(new BankAccountTest("testWithdraw"));
}
}
}
Main.as:
![程序代码](http://www.wolf2999.cn/images/code.gif)
package
{
import asunit.textui.TestRunner;
import flash.display.Sprite;
public class Main extends Sprite
{
public function Main()
{
var unittests:TestRunner = new TestRunner();
stage.addChild(unittests);
unittests.start(AllTests, null, TestRunner.SHOW_TRACE);
}
}
}
{
import asunit.textui.TestRunner;
import flash.display.Sprite;
public class Main extends Sprite
{
public function Main()
{
var unittests:TestRunner = new TestRunner();
stage.addChild(unittests);
unittests.start(AllTests, null, TestRunner.SHOW_TRACE);
}
}
}
将测试类中:
![程序代码](http://www.wolf2999.cn/images/code.gif)
public function testDeposit():void {
instance.deposit(50);
assertTrue("Balance on a new account after 50 deposit is 50", instance.getBalance() == 50);
instance.deposit(25);
assertEquals("Balance after 50 deposit and another 25 deposit is 75", 75,instance.getBalance());
}
instance.deposit(50);
assertTrue("Balance on a new account after 50 deposit is 50", instance.getBalance() == 50);
instance.deposit(25);
assertEquals("Balance after 50 deposit and another 25 deposit is 75", 75,instance.getBalance());
}
instance.deposit(25);改成instance.deposit(125);
运行测试用例将会出现设定的报错字符。
源码在examplesmyself.rar
examplesmyself:http://www.wolf2999.cn/down/examplesmyself.rar
asunit:http://www.wolf2999.cn/down/asunit.rar
flexunit:http://www.wolf2999.cn/down/flexunit.rar