530 likes | 689 Views
運用測試技術提昇軟體生產力. Microsoft .NET 技術代言人 林耀珍. 林耀珍. 經歷 第三波資訊 技術總監 育碁數位科技 總經理 專業認證與專長 微軟 .NET 技術 軟體開發流程,資訊系統規劃 Microsoft MCSD/MCSE/MCDBA 物件導向技術, Rational OOAD 認證講師 Lotus Notes principle CLP/CLI J2EE. 目標與對象. 對象 技術平台的決策人員 軟體建構師 系統分析師 專案經理 應用程式開發人員 目標 快速開發高品質的應用程式. Agenda.
E N D
運用測試技術提昇軟體生產力 Microsoft .NET 技術代言人 林耀珍
林耀珍 • 經歷 第三波資訊 技術總監 育碁數位科技 總經理 • 專業認證與專長 微軟 .NET 技術 軟體開發流程,資訊系統規劃 Microsoft MCSD/MCSE/MCDBA 物件導向技術,Rational OOAD 認證講師 Lotus Notes principle CLP/CLI J2EE .NET技術代言人 林耀珍 2004/05/31
目標與對象 對象 • 技術平台的決策人員 • 軟體建構師 • 系統分析師 • 專案經理 • 應用程式開發人員 目標 • 快速開發高品質的應用程式 .NET技術代言人 林耀珍 2004/05/31
Agenda • Overview • Design .NET Components • Unit Test Using NUnit • Code review • Web Application Functional Test .NET技術代言人 林耀珍 2004/05/31
Mars Climate Orbiter • Purpose: • to relay signals from the Mars Polar Lander once it reached the surface of the planet • Disaster: • smashed into the planet instead of reaching a safe orbit • Why: • Software bug - failure to convert English measures to metric values • Cost: $165M .NET技術代言人 林耀珍 2004/05/31
Shooting Down of Airbus 320 • 1988 US Vicennes shot down Airbus 320 Mistook airbus 320 for a F-14, 290 people dead • Why: • Software bug - cryptic and misleading output displayed by the tracking software .NET技術代言人 林耀珍 2004/05/31
THERAC-25 Radiation Therapy • THERAC-25: • A computer-controlled radiation-therapy machine • 1986: • two cancer patients at the East Texas Cancer Center in Tyler received fatal radiation overdoses • Why: • Software bug - mishandled race condition (i.e., miscoordination between concurrent tasks) .NET技術代言人 林耀珍 2004/05/31
Expectation • Can’t we expect software to execute correctly? • The answer is ? • Carefully made programs have 5 faults/1000 LOC • 10,000 LOC will have 50 faults • 100,000 LOC will have 500 faults • 1,000,000 LOC will have 5000 faults • Pass/Fail Ratio of Software project • ~ 75% : 25% • Why not remove the faults? .NET技術代言人 林耀珍 2004/05/31
Quality or Productivity There should have some problems ? Programmers develop code and check into a common repository. The QA team builds the application and tests it after few hours or a day passed. Defects and issues are reported back to the programmer, describing the offending behavior. The programmer spends 4 hours tracking down exactly where the bug occurs in the code, only to discover a simple error which takes 15 seconds to fix. The code is checked back in, and the cycle repeats. .NET技術代言人 林耀珍 2004/05/31
T T S Waterfall Seem near, but far ! Reqs Arch Design Code Intg Test .NET技術代言人 林耀珍 2004/05/31
Write a test Refactor code(and test) Compile T Run test, watch it pass Fix compile errors T Write code Run test,watch it fail S Test Driven Development • Seem far, but near !!! .NET技術代言人 林耀珍 2004/05/31
Some Misconception • Responsibility • The testing team is responsible for assuring quality • Everyone in development has responsibility for quality • When • Starting testing after design/code is completed • Designing tests before coding begins • Skill • Use testing as a transitional job for new programmers • Good testers require deep knowledge and experience • Productivity • Have no time to do testing • Defects and issues will draw you back .NET技術代言人 林耀珍 2004/05/31
T T S Who should write the tests ? • The programmers should write the tests • The programmers can’t wait for somebody else to write tests • For customers & yourself .NET技術代言人 林耀珍 2004/05/31
Goal of Test • Reduce Risk • Increase productivity by ensuring • Correctness • Reliability • Usability • Robustness • Performance .NET技術代言人 林耀珍 2004/05/31
Types of Testing • Execution-based testing • Non-execution based testing • Code-Review .NET技術代言人 林耀珍 2004/05/31
Agenda • Overview • Design .NET Components • Unit Test Using NUnit • Code review • Web Application Functional Test .NET技術代言人 林耀珍 2004/05/31
Unit Tests • Automatic • they check their own results) • Repeatable • able to be run again and again, by multiple people • Available • they should accompany the code they test, so anybody who has that code can run them .NET技術代言人 林耀珍 2004/05/31
Where we are ? • Unit Test .NET技術代言人 林耀珍 2004/05/31
Top-Down or Bottom-Up • Top-Down • Have to add mocked object • Bottom-Up • Have to provide stub Module-A Module-A Stub Module-B Mocked Object Module-B .NET技術代言人 林耀珍 2004/05/31
The straight way • Create a test method • For each tested method • Setup initial value for each variable and data store • Execute tested method • Verify result • Show pass or fail • Clean up variables and data store .NET技術代言人 林耀珍 2004/05/31
Money Class public class Money { private double fAmount; private String fCurrency; public Money Add(double amount) { fAmount += amount; return this; } public override bool Equals(Object anObject) { ... } public Money Multiply(double factor) { … } public Money Subtract(IMoney m) { … } } .NET技術代言人 林耀珍 2004/05/31
Test Money using Console App public void DoTest() { Money m; SetUp(); m = f12NT.Add(14); if (m.Equals(new Money(26, "NT"))) Console.WriteLine("Ok :Test Add()"); else Console.WriteLine("Nok : Test Add()"); SetUp(); m = f14NT.Subtract(12); … SetUp(); m = f14NT.Multiply (1000.0 / 3.0 ); … } .NET技術代言人 林耀珍 2004/05/31
Using NUnit • Add a Class Library project for Testing • Add Ref: nunit.framework • Add [TextFixture] to TestClass • Add [Test] to TestMethod • Compile Test project • Open Nunit-Gui.exe • Open TestProject.dll or Drag into Nunit-Gui • Run .NET技術代言人 林耀珍 2004/05/31
TestMoney using Nunit using nunit.framework; [TestFixture] public class TestMoney { private Money f12NT; private Money f14NT; [Test] public void Add() { Money m = f12NT.Add(f14NT); Assert.AreEqual(m.Amount, 26); } [Test] public void Multiply() { Money m = f14NT.Multiply (1000.0 / 3.0 ); Assert.AreEqual(m.Amount, 4666.667, 0.001); } .NET技術代言人 林耀珍 2004/05/31
SetUp & TearDown Method • Setup is Called before each test is run • TearDown is Called after each test is run [SetUp] public void SetUp() { f12NT= new Money(12, "NT"); f14NT= new Money(14, "NT"); } [TearDown] Public void Cleanup() {… } .NET技術代言人 林耀珍 2004/05/31
Nunit-Gui • Nunit has Console and Gui version .NET技術代言人 林耀珍 2004/05/31
Nunit run result • Green, yellow, red .NET技術代言人 林耀珍 2004/05/31
Assertions • Various forms • Assert( bool ) • AssertNull( object ) • AssertNotNull( object ) • AssertSame( object, object ) • AssertEquals( object, object ) • AssertEquals( int, int ) • AssertEquals( float, float, float ) • AssertEquals( double, double, double ) • May also take a message as first argument .NET技術代言人 林耀珍 2004/05/31
Work within VS.NET • TestRunner • http://www.mailframe.net/Products/TestRunner.htm • NUnitAddIn • http://sourceforge.net/projects/nunitaddin • Debug .NET技術代言人 林耀珍 2004/05/31
Organize Tests • Test • Test Class • Test Suite • Namespace .NET技術代言人 林耀珍 2004/05/31
Organize Tests with namespace .NET技術代言人 林耀珍 2004/05/31
Code Review • Follow .NET Component Design Guidelines • msdn.microsoft.com/library/en-us/cpgenref/html/ cpconnetframeworkdesignguidelines.asp • Use FxCop • http://www.gotdotnet.com/team/fxcop/ .NET技術代言人 林耀珍 2004/05/31
.NET Design Guidelines - I • Naming conventions and coding guidelines ensure a consistent code base • For example (.NET Design Guidelines): • For public interfaces, use PascalCasing • For private members, use camelCasing • Use underscore “_” character to denote private class members • Use camelCasing for all method parameters .NET技術代言人 林耀珍 2004/05/31
.NET Design Guidelines - II Class Members Usage • Hide the implementation of a class • Avoid public member variables • Avoid write-only properties • Avoid complex logic in properties • Allow properties to be set in any order • Design for inheritance .NET技術代言人 林耀珍 2004/05/31
.NET Design Guidelines - III Error Raising and Handling • Exceptions are not for flow of control! • Exceptions are “exceptional” • Derive new custom exceptions from the ApplicationException class .NET技術代言人 林耀珍 2004/05/31
Use FxCop • New project • Add DLL by D&D or “Project/Add Targets” menu • Analysis .NET技術代言人 林耀珍 2004/05/31
Unit Test Design Considerations • When to Write Test Code • Unit tests should be written concurrently with, immediately after, or even before you write the production code. • never check code into the team's version control system without passing unit tests • Where to Keep Test Code • It depends on the size of project, security, … • How Not to Break Encapsulation • Test class has to access to private functions • Possible solution: let them sit in the same assembly, and declare “internal” functions for TestFixture, and have an intelligent build mechanism. .NET技術代言人 林耀珍 2004/05/31
Hard Stuff • Testing connected sets of objects is hard • Tests become dependent on one another • Objects have to be put in a known state • Hard to figure out what failed • Testing UI is hard • Hard to separate from other parts of the app • Not separating increases overall coupling .NET技術代言人 林耀珍 2004/05/31
Agenda • Overview • Design .NET Components • Unit Test Using NUnit • Code review • Web Application Functional Test .NET技術代言人 林耀珍 2004/05/31
Where we are ? • Functionality • UI .NET技術代言人 林耀珍 2004/05/31
Simplified Procedures • Assume you know the whole function specs • Create UI • Implement main flow of events • Manual test until stabilize • Create Tests • Be able to sustain when UI Layout is changed • Add more functionalities & Tests .NET技術代言人 林耀珍 2004/05/31
Create UI .NET技術代言人 林耀珍 2004/05/31
Implement main flow of events private void AddButton_Click(object sender, System.EventArgs e) { MoneyBag myMoneyBag = GetMyMoneyBag(); Money m = GetInputMoney(); myMoneyBag.AddMoney(m); DisplayMoneyBag(); } .NET技術代言人 林耀珍 2004/05/31
Manual test until stabilize .NET技術代言人 林耀珍 2004/05/31
Create Tests • The Hard Way • Code test programs • The less hard way • Coding with NunitAsp • http://sourceforge.net/projects/nunitasp/ • The other less hard way • Using Visual Testing Tool .NET技術代言人 林耀珍 2004/05/31
NunitAsp • Download • Unzip to any folder • Test project • Addref to NunitAsp-folder/bin/NUnitAsp.dll • Using namespace • using NUnit.Extensions.Asp; • using NUnit.Extensions.Asp.AspTester; • Create Mocked asp.net contorls • Browse page • Click button • Assert result .NET技術代言人 林耀珍 2004/05/31
Supported Contorls .NET技術代言人 林耀珍 2004/05/31
Code Test method using NunitAsp [Test] public void AddNTMoney() { CurrencyList.SelectedIndex=0; AmountTextBox.Text="100"; AddButton.Click(); AssertEquals("NT", DataGrid1.TrimmedCells[0][0]); AssertEquals("100.00", DataGrid1.TrimmedCells[0][1]); } //[SetUp] protected override void SetUp() { MockWebControls(); Browser.GetPage(defaultPage); } .NET技術代言人 林耀珍 2004/05/31
Using Visual Testing Tool • Record • Browse • Input • Submit • Verify return result • Close browser • Adjust • Reorganize .NET技術代言人 林耀珍 2004/05/31
It is just beginning ! • Testing Process • Planning – involves Test Plans • Iteration, Configuration • Designing – involves Test cases • Preconditions, Postconditions • Input, function, expected output • Implementing – involves Test scripts , Test suites • Manual or Automatic • Executing • Resources allocation, DB preparations • evaluating & Reporting .NET技術代言人 林耀珍 2004/05/31