1 / 69

Component Testing ( Unit Testing )

Component Testing ( Unit Testing ). Introduction to Unit Testing. Nikolay Kostov. Technical Trainer. http://www.nikolay.it. Telerik QA Academy. http://qaacademy.telerik.com. Table of Contents. What is Unit Testing ? Code and Test vs. Test Driven Development Unit testing Frameworks

radley
Download Presentation

Component Testing ( Unit Testing )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Component Testing (Unit Testing) Introduction to Unit Testing Nikolay Kostov Technical Trainer http://www.nikolay.it Telerik QA Academy http://qaacademy.telerik.com

  2. Table of Contents • What is Unit Testing? • Code and Test vs. Test Driven Development • Unit testing Frameworks • Visual Studio Team Test • Nunit • Gallio • Unit Testing Best Practices

  3. What is Unit Testing?

  4. Unit Test – Definition A unit test is a piece of code written by a developer that exercises a very small, specific area of functionality of the code being tested. “Program testing can be used to show the presence of bugs, but never to show their absence!” Edsger Dijkstra, [1972]

  5. Manual Testing • You have already done unit testing • Manually, by hand • Manual tests are less efficient • Not structured • Not repeatable • Not on all your code • Not easy to do as it should be

  6. Unit Test – Example int Sum(int[] array) { sum = 0; for (int i=0; i<array.Length; i++) sum += array[i]; return sum; } void TestSum() { if (Sum(new int[]{1,2}) != 3) throw new TestFailedException("1+2 != 3"); if (Sum(new int[]{-2}) != -2) throw new TestFailedException("-2 != -2"); if (Sum(new int[]{}) != 0) throw new TestFailedException("0 != 0"); }

  7. Unit Testing – Some Facts • Tests are specific pieces of code • In most cases unit tests are written by developers, not by QA engineers • Unit tests are released into the code repository along with the code they test • Unit testing framework is needed • Visual Studio Team Test (VSTT) • NUnit, MbUnit, Gallio, etc.

  8. Unit Testing – More Facts • All classes should be tested • All methods should be tested • Trivial code may be omitted • E.g. property getters and setters • Ideally all unit tests should pass before check-in into the source control repository

  9. Why Unit Tests? • Unit tests dramatically decrease the number of defects in the code • Unit tests improve design • Unit tests are good documentation • Unit tests reduce the cost of change • Unit tests allow refactoring • Unit tests decrease the defect-injection rate due to refactoring / changes

  10. Code and Test vs. Test Driven Development

  11. Unit Testing Approaches • "Code and Test" approach • Classical approach • "Test First" approach • Test-driven development (TDD)

  12. Code and Test Approach Write code Write unit test Run and succeed Time flow

  13. Test Driven Development (TDD) Create a testlist Pick а test Write test Compile and fail Write enough code to compile Run test and fail Time flow Write code to pass test Remove duplication

  14. Why Test Driven Development? • Helps find design issues early and avoids rework • Writing code to satisfy a test is a focused activity – less chance of error • Tests will be more comprehensive than when written after code

  15. Unit Testing Frameworks

  16. Unit Testing Frameworks • JUnit • The first popular unit testing framework • Based on Java, written by Kent Beck & Co. • Similar frameworks have been developed for a broad range of computer languages • NUnit – for C# and all .NET languages • cppUnit, jsUnit, PhpUnit, PerlUnit, ... • Visual Studio Team Test (VSTT) • Developed by Microsoft, integrated in VS

  17. Visual Studio Team Test(VSTT)

  18. Visual Studio Team Test – Features • Team Test (VSTT) is very well integrated with Visual Studio • Create test projects and unit tests • Execute unit tests • View execution results • View code coverage • Located in the assembly Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll

  19. Visual Studio Team Test – Attributes • Test code is annotated using custom attributes: • [TestClass] – denotes a class holding unit tests • [TestMethod] – denotes a unit test method • [ExpectedException] – test causes an exception • [Timeout] – sets a timeout for test execution • [Ignore] – temporary ignored test case • [ClassInitialize], [ClassCleanup]– setup / cleanup logic for the testing class • [TestInitialize], [TestCleanup]– setup / cleanup logic for each test case

  20. Assertions • Predicate is a true / false statement • Assertion is a predicate placed in a program code • Indicates that the developer thinks that the predicate is always true at that place • If an assertion fails, the method call does not return and an error is reported • Example: • Assert.AreEqual(expectedValue, actualValue, • "Error message.");

  21. VSTT – Assertions • Assertions check condition and throw exception if condition is not satisfied • Comparing values • AreEqual(expected value, calculated value [,message]) – compare two values for equality • Comparing objects • AreSame(expected object, current object [,message])– compares object references

  22. VSTT – Assertions (2) • Checking for null value • IsNull(object [,message]) • IsNotNull(object [,message]) • Conditions • IsTrue(condition) • IsFalse(condition) • Forced test fail • Fail(message)

  23. The 3A Pattern • Arrange all necessary preconditions and inputs • Act on the object or method under test • Assert that the expected results have occurred • [TestMethod] • public void TestDeposit() • { • BanckAccount account = new BanckAccount(); • account.Deposit(125.0); • account.Deposit(25.0); • Assert.AreEqual(150.0, account.Balance, • "Balance is wrong."); • }

  24. Code Coverage • Shows what percent of the code we’ve covered • We may have pointless unit tests that are calculated in the code coverage • 70-80% of coverage is excellent

  25. VSTT – Example public class Account { private decimal balance; public void Deposit(decimal amount) { this.balance += amount; } public void Withdraw(decimal amount) { this.balance -= amount; } public void TransferFunds( Account destination, decimal amount) { … } public decimal Balance { … } }

  26. VSTT – Example (2) using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] public class AccountTest { [TestMethod] public void TransferFunds() { Account source = new Account(); source.Deposit(200.00M); Account dest = new Account(); dest.Deposit(150.00M); source.TransferFunds(dest, 100.00M); Assert.AreEqual(250.00M, dest.Balance); Assert.AreEqual(100.00M, source.Balance); } }

  27. VSTT – Screenshot

  28. Visual Studio Team Test Live Demo

  29. NUnit http://www.nunit.org/index.php?p=download

  30. NUnit – Features • Test code is annotated using custom attributes • Test code contains assertions • Tests organized as multiple assemblies • Two execution interfaces • GUI: nunit-gui.exe • Console: nunit-console.exe

  31. NUnit – Features (2) • NUnitprovides: • Creating and running tests as NUnit Test Projects • Visual Studio support

  32. NUnit – Attributes • [TestFixture] – Marks a class that contains tests • Should be a public class • [Test] – Marks a method as an unit test • [TestFixtureSetUp] – Indicate a setup method that will be ran once before all other tests • Called before the tests are started

  33. NUnit – Attributes (2) • [TestFixtureTearDown] – Indicate a tear down method that will be ran once after all other tests have run • Called after all the tests have finished • [SetUp]– indicates a setup method that should be ran before each of the tests • [TearDown] – used to indicate a tear down that method should be ran after each of the tests

  34. NUnit – Attributes (3) • [ExpectedException(typeof(Exception))] • When you expect an exception to be thrown • Will only pass if exception type was thrown • [Ignore("Not ready for primetime")] • Indicates test that is not ready, or should not be run

  35. NUnit – Assertions • Using NUnit.Framework.Assert class • Assertions check condition and throw exception if condition is not satisfied • Comparing values • AreEqual(expectedvalue, calculated value [, message]) - compare values

  36. NUnit – Assertions (2) • Comparing objects • IsNull(object [, message]) • IsNotNull(object [, message]) • AreSame(expected object, current object [,message]) – compare object references • Conditions • IsTrue(condition) • IsFalse(condition)

  37. NUnit – Assertions (3) • Forced test fail • Fail() • Explicit tests (ignored unless run explicitly) • [TestFixture, Explicit] • [Test, Explicit] • Categories • [TestFixture, Category("LongRunning")] • [Test, Category("Long")]

  38. NUnit – Example: Class public class Account { private float balance; public void Deposit(float amount) { balance+=amount; } public void Withdraw(float amount) { balance-=amount; } public void TransferFunds( Account destination, float amount) { … } public float Balance { get { return balance; } } }

  39. NUnit – Example: Test using NUnit.Framework; [TestFixture] public class AccountTest { [Test] public void TransferFunds() { Account source = new Account(); source.Deposit(200.00F); Account dest = new Account(); dest.Deposit(150.00F); source.TransferFunds(dest, 100.00F); Assert.AreEqual(250.00F, dest.Balance); Assert.AreEqual(100.00F, source.Balance); } }

  40. SetUp & TearDown Timeline • Begin Tests • Call TestFixtureSetUp • Call SetUp • Call ExampleTestOne • Call TearDown • Call SetUp • Call ExampleTestTwo • Call TearDown • Call TestFixtureTearDown • End Tests

  41. NUnit – Screen Shots

  42. NUnit – Screen Shots (2)

  43. NUnit – Screen Shots (3)

  44. NUnit – Screen Shots (4)

  45. Gallio

  46. The Gallio Automation Platform • The Gallio Automation Platform • An open, extensible, and neutral system for .NET using many test frameworks • Gallio can run tests from MbUnit, MSTest, NUnit, xUnit.Net, csUnit, and RSpec • Provides a common object model, runtime services and tools (such as test runners) • May be leveraged by any number of test frameworks • www.gallio.org

  47. Interfaces • Gallio includes its own interfaces: • Echo • Command-line runner • Icarus • Windows GUI

  48. Gallio – Screen Shots

  49. Gallio – Screen Shots (2)

  50. Gallio – Screen Shots (3)

More Related