Docsity

Create Unit Testing Project using Visual Studio, Exams of Software Development

Toggle sidebar
bg1
Unit Testing in Visual Studio 2008
Introduction
This article will show how the Unit Testing in VS2008 works and what features are available to
everyone.
Background
There are other Unit Testing tools that perform similar functions. One that I have used in the past
as many of you have used as well is NUnit. There are other ones that do deviations of Unit
Testing as well, such as MBUnit.
Adding the Tests
Once you have a project that works and has class files, you can create a Unit Test. To start, we
need to create the basic tests. A Unit Test can be created from the Test menu.
Once you select the menu item, this dialog will be displayed
pf3
pf4
pf5

Partial preview of the text

Download Create Unit Testing Project using Visual Studio and more Exams Software Development in PDF only on Docsity!

Unit Testing in Visual Studio 2008

Introduction

This article will show how the Unit Testing in VS2008 works and what features are available to everyone.

Background

There are other Unit Testing tools that perform similar functions. One that I have used in the past

as many of you have used as well is NUnit. There are other ones that do deviations of Unit Testing as well, such as MBUnit.

Adding the Tests

Once you have a project that works and has class files, you can create a Unit Test. To start, we

need to create the basic tests. A Unit Test can be created from the Test menu.

Once you select the menu item, this dialog will be displayed

The options are:

  • Ordered Test โ€“ this will create a test where you set the order of the Unit Tests you desire
  • Unit Test โ€“ this will create a .cs file; you can then decide what tests you want done
  • Unit Test Wizard โ€“ A wizard will appear so you can select what classes you want to have in your Unit Test

Unit Test Wizard

If you select the Unit test wizard, this dialog will appear so you can select what classes will be tested:

Once you select the methods, the wizard will create the source file(s). There will be a source file

for each class that you have selected to be tested. An example of the source code the wizard generates is shown below:

///

///A test for RescanAlbumId /// // TODO: Ensure that the UrlToTest attribute // specifies a URL to an ASP.NET page (for example, // http://.../Default.aspx). This is necessary // for the unit test to be executed on the web server, // whether you are testing a page, web service, or a WCF service. [TestMethod()] [HostType("ASP.NET")] [AspNetDevelopmentServerHost("C:\MyProjects\backup\Simple\SimplePhotos", "/SimplePhotos")] [UrlToTest("http://localhost/SimplePhotos")] public void RescanAlbumIdTest() { // TODO: Initialize to an appropriate value Rescan_Accessor target = new Rescan_Accessor(); int Albumid = 0; // TODO: Initialize to an appropriate value int expected = 0; // TODO: Initialize to an appropriate value int actual; actual = target.RescanAlbumId(Albumid); Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); }

As you can see, the arguments are set with default values; for the test to be completely effective, you must fill in the values that fit your requirement. After you set the correct values, you can run

your project as a normal project. The final testing results will appear in the window as shown below.

Unit Tests

The Unit Test option will create a source file that will contain the following code snippet: [TestMethod] public void TestMethod1() { // // TODO: Add test logic here // } You must fill TestMethod1 to do the tests you need to perform. You can add more tests to test

the methods you want tested. This is very similar to NUnit where you write what tests you want to have done.

Ordered Test

When this option is selected, this dialog will appear so you can select what test(s) you want to run and in what sequence: