34
An Introduction to Unit Test Using NUnit Wei Li 08/2007

An Introduction to Unit Test Using NUnit

Embed Size (px)

DESCRIPTION

An introduction to unit test using NUnit

Citation preview

Page 1: An Introduction to Unit Test Using NUnit

An Introduction to Unit Test Using NUnitUsing NUnit

Wei Li

08/2007

Page 2: An Introduction to Unit Test Using NUnit

Test, Test and More Test

• Developer Testo Unit Testo Integration Testo Regression Test

• QA Test• QA Testo Integration Testo Regression Testo Performance Testo Stress Test

• Customer/User Testo Acceptability Testo Useability Test

Page 3: An Introduction to Unit Test Using NUnit

What Is Unit Test

• Self checking or validation to ensure your code works as expected

• Developer’s test

• Part of the construction cycle

Page 4: An Introduction to Unit Test Using NUnit

Where Does Unit TestSit In Life Cycle

• Requirement Gathering

• Business System Design

• Technical Design

o Architecture Designo

o Technical System Design

• Construction

oCode and Unit Test• QA Test

• User Test

Page 5: An Introduction to Unit Test Using NUnit

Unit Test Is Not

• QA test

• Customer/user test• Customer/user test

Page 6: An Introduction to Unit Test Using NUnit

Why Unit Test

• What? Write code to test code?

• Why?• Why?

Page 7: An Introduction to Unit Test Using NUnit

Instant Feedback

• Unit test provides the earliest opportunity to identify and fix a defect

• The earlier a defect is caught, the less • The earlier a defect is caught, the less expensive it is to fix it

• Done before integration

• Like a dedicated QA/user sits next to you and test your code immediately

Page 8: An Introduction to Unit Test Using NUnit

Help Design And Write Better Code

• If your code is hard to be unit tested, how can it be easily used, maintained and extended?extended?

Page 9: An Introduction to Unit Test Using NUnit

Regression Checker

• When new release of the external dependency comes, you can quickly run the unit tests to make certain the new release won’t break your system

• Automatic change detector – When changes are made to the code, automated unit

tests ensure that the change does not break something somewhere

Page 10: An Introduction to Unit Test Using NUnit

Good Way To Work On Legacy Code

• Write unit test to get started with legacy code

Page 11: An Introduction to Unit Test Using NUnit

Good Way To Learn New Language and New API

• Write unit test cases to learn and try new API

• Your unit test cases become reusable knowledge base

Page 12: An Introduction to Unit Test Using NUnit

Good For Reporting a Defect

• Write a test case to report a defect or bug in a system

• When something fails, writing a unit test for that failure guarantees that it's fixed for once and for all.

Page 13: An Introduction to Unit Test Using NUnit

Save Time In Long Term

• Unit test is reusable and repeatable

• Unit test can be automated• Unit test can be automated

• Write once run forever

Page 14: An Introduction to Unit Test Using NUnit

There Is More Than One Way To Do It

• Main() method with lots of print out

• GUI “push that button” test• GUI “push that button” test

• Debugger test

Page 15: An Introduction to Unit Test Using NUnit

Problems With The Above Methods

• Is the test well structured?

• Can it be automated?

• Is the test repeatable?• Is the test repeatable?

• Does it need human interaction?

• Is it easy to be maintained ?

• How to present test results?

Page 16: An Introduction to Unit Test Using NUnit

Stop Debugger Test

• Debugger time is not recyclable

• Debugger session is not reusable by others• Debugger session is not reusable by others

• Not a regression testing tool

• Mental assertion is error-prone and boring

Page 17: An Introduction to Unit Test Using NUnit

Enter NUnit

• A unit test framework written in C#

• It uses attributes to identify unit test fixtures

• It uses assertions for verification• It uses assertions for verification

• It can be used to test any .NET code, not just C#

• It provides a console and GUI interface

• It can be integrated into VS.NET

Page 18: An Introduction to Unit Test Using NUnit

How Does NUnit Test Code Look Like?

• Show me the code

Page 19: An Introduction to Unit Test Using NUnit

NUnit Attributes

• Custom attribute injects more information to your class at compilation time

• NUnit uses it to mark and identify unit test fixture

Page 20: An Introduction to Unit Test Using NUnit

NUnit Required Attributes

– [TestFixture]• Used to indicate that a class contains test methods

– [Test]• Used to indicate that a method within a test fixture

should be run by the Test Runner application

Example: Test1.cs

Page 21: An Introduction to Unit Test Using NUnit

NUnit Optional Attributes

– [SetUp] • Used to indicate a setup method should be ran before each of

the tests

– [TearDown] – [TearDown] • Used to indicate a tear down method should be ran after each

of the tests are ran

Example: Test2.cs

Page 22: An Introduction to Unit Test Using NUnit

NUnit Optional Attributes

– [TestFixtureSetUp]• Used to indicate a setup method that will be ran once; before

all other tests. This is the first method that is called before the tests are started.

– [TestFixtureTearDown]• Used to indicate a tear down method that will be ran once;

after all other tests have run. This is the last method that is called after all the tests have finished.

Example: Test3.cs

Page 23: An Introduction to Unit Test Using NUnit

NUnit Optional Attributes

– [ExpectedException(typeof(Exception))] • When you want an exception to be thrown• Will only pass if exception type was thrown

– [Ignore(“Not ready yet")]– [Ignore(“Not ready yet")]

Example: Test4.cs

Page 24: An Introduction to Unit Test Using NUnit

NUnit Assertion

• It is all unit test cares about

• NUnit provides a full set of assertions ready • NUnit provides a full set of assertions ready to be used

• Assertion failure means test failure

Page 25: An Introduction to Unit Test Using NUnit

NUnit Assertion

• Equality Asserts

• Identity Asserts

• Comparison Asserts• Comparison Asserts

• Type Asserts

• Condition tests

• Utility methods

Page 26: An Introduction to Unit Test Using NUnit

NUnit Equal Assertion

• Assert.AreEqual(expected, real_value)

• Assert.AreEqual(expected, real_value, “a message”)“a message”)

• Assert.AreNotEqual(expected, real_value)

• Assert.AreNotEqual(expected, real_value, “a message”)

Page 27: An Introduction to Unit Test Using NUnit

NUnit Identity Assertion

• Assert.AreSame(expected, real_value)

• Assert.AreSame (expected, real_value, “a message”)“a message”)

• Assert.AreNotSame (expected, real_value)

• Assert.AreNotSame (expected, real_value, “a message”)

Page 28: An Introduction to Unit Test Using NUnit

NUnit Condition Assertion

• Assert.IsTrue()

• Assert.IsFalse()

• Assert.IsNull()• Assert.IsNull()

• Assert.IsNotNull()

• Assert.IsEmpty()

• Assert.IsNotEmpty()

Page 29: An Introduction to Unit Test Using NUnit

Examples

Show me the code

Page 30: An Introduction to Unit Test Using NUnit

Test Driven/Test First Development

• Write a unit test case for a new functionality

• Run the unit test and it will fail

• Write just enough code to make the test pass• Write just enough code to make the test pass

• Run the unit test again and it passes

• Refactory the code to make it better

• Repeat the unit test

• Repeat the cycle

Page 31: An Introduction to Unit Test Using NUnit

Continuous Integration

• Get the latest version of the project from source control system

• Build/compile the code• Run all unit test cases• Run all unit test cases• Publish the build and test results• Notify the development team for any failure• Repeat the process periodically, for example, once

every hour

Page 32: An Introduction to Unit Test Using NUnit

Continuous Integration

• Demo: using NAnt to build solution, run test cases, generate test result reports

Page 33: An Introduction to Unit Test Using NUnit

Questions?

Page 34: An Introduction to Unit Test Using NUnit

Happy Testing!