Unit Testing - VSTS
Unit Testing - VSTS
Visual Studio 2005 Team System is a productive, integrated, and extensible suite of
lifecycle tools that enable greater communication and collaboration among software
development teams.
A client tool for those devoted to testing that enables them to design, manage, and
execute manual, unit and load tests. It is designed to aid in the process of software
development. VSTT enables
Team Test
VSTS takes testing a step further than simple unit testing by making them available
in the Team Test product geared specifically for testers. This product enables testers
to execute the unit tests developers create in a systematic fashion to ensure that
code checked in by multiple developers’ passes previously constructed tests. This is
an example of another testing best practice referred to as regression testing.
Team Test goes even further by making testers first-class citizens of the project by
highlighting the importance of testing at the team level through a test authoring and
execution environment. That environment includes additional test types such as Web
Tests, Load Tests (similar to the tool Application Center Test tool that ships with
Visual Studio .NET 2003), Manual Tests, and Ordered Tests and test case
management all integrated into Team Foundation.
Advantages/Benefit of Unit Tests
• The unit tests serve as the first users of your system and will frequently identify
design issues or functionality that is lacking.
• A well-written test suite provides the original developer with the freedom to pass the
system off to other developers for maintenance and further enhancement.
• Cheaper cost - It is a component that comes along with the MS Visual Studio
IDE.
• Disciplined Development - Well defined deliverable for the developer and more
quantifiable progress. Often, just considering a test case will identify issues
with an approach/design.
• Facilitates change and Reduces code fragility – Unit testing allows the
programmer to refractor code at a later date, and make sure the module still
works correctly (i.e. regression testing).
• Relative cost to fix defects graph - The earlier an error is caught the cheaper
it is to fix.
It takes the typical developer time and practice to become comfortable with unit
testing. Once a developer has been saved enough time by unit tests, he or she will
latch on to them as an indispensable part of the development process.
Unit testing does require more explicit coding, but this cost will be recovered, and
typically exceeded, when you spend much less time debugging your application.
• Always separate your unit test assemblies from the code you are testing.
• Avoid altering the code you are testing solely to allow easier unit testing.
• Each test should verify a small slice of functionality. Do not write long
sequential unit tests that verify a large number of items.
• All tests should be autonomous. Avoid creating tests that rely on other tests
to be run beforehand.
• Test both expected behavior (normal workflows) and error conditions
(exceptions and invalid operations).
Test-driven development
Test-driven development (TDD) is the practice of writing unit tests before writing the
code that will be tested. TDD encourages following a continuous cycle of
development involving small and manageable steps.
The most common way to determine success in unit tests is to compare an expected
result against an actual result. The Assert class features many methods that enable
you to make these comparisons quickly.
Assert.AreEqual, Assert.AreNotEqual
Assert.AreNotSame, Assert.AreSame
Assert.Equals
Assert.Fail
Assert.Inconclusive
Assert.IsFalse
Assert.IsInstanceOfType
Assert.IsNotInstanceOfType
Assert.IsNotNull
Assert.IsNull
Assert.IsTrue
• TestClass
• TestMethod
• Description
• Ignore
• ExpectedException
2. By default, the Output project setting is for a new Visual Basic project, but C#
and C++ test projects are also available. We will select Visual C# and click
the OK button, followed by entering a project name.
Example:
Click Start > Programs > Microsoft Visual Studio 2005 >Microsoft Visual Studio 2005
4. Create Unit Test for "add" method by right clicking on the method
5. This will display a dialog box for generating unit tests into a different project (see
Figure below). By default, the Output project setting is for a new Visual Basic
project, but C# and C++ test projects are also available. For this article, we will
select as C# project click the OK button.
6. Now enter a Test project name of computeTest for the project name.
7. The generated test project contains following files related to testing.
8. In addition to some default files; the generated test project contains references to
both the Microsoft.VisualStudio.QualityTools.UnitTestFramework and the computation
project, which the unit tests are executing against. The former is the testing
framework assembly the test engine depends on when executing the unit tests. The
latter is a project reference to the target assembly we are testing.
There are two important attributes related to testing with Team Test.
Microsoft.VisualStudio.QualityTools.UnitTesting.Framework namespace.
Team Test uses reflection to search a test assembly and find all the TestClass
decorated classes, and then find the corresponding TestMethodAttribute decorated
methods to determine what to execute.
One other important criterion, validated by the execution engine but not the
compiler, is that the test method signature be an instance method that takes no
parameters.
The test method AddTest() instantiates the target computaion class before checking
for assertion.
Note that the checks are done using the Assert.AreEqual() method.
To view additional details on the test, we can double click on it to open up the
AddTest() Results:
Please note that the message in the assert statement does not appear for a
"passing" test. It appears only when a test fails along with "Expected" and "Actual"
values. Lets assume that by mistake (even though it’s highly unlikely) we have put
total=x*y instead of x+y in source code file. Let's try to run the same test and see
the results:
How to do a Web testing for a login page
In Test menu click on Windows > Test View. In Test View, right Click on method
name --> Properties --> Chose Data Connection String, Data Table Name and Data
Access method as sequential or random.
2. Creating a Test Project.
We create the test project just as we would any VSTS unit testing project. The steps
below outline the process. In order to successfully record a test, the website must be
running.
1. Right-click the project, click Add, and then click Web Test. This opens up a
browser with a special Web Test Recorder explorer bar.
Recording a Web test case
2. On the address bar, enter the URL of the login web page, including the port
selected by the ASP.NET Development Server. Browsing to this location will be
recorded in the Web Test Recorder explorer bar as would any other URLs that
are entered.
3. Enter the user name and password that were added earlier. Upon clicking the
Login button, another entry will be recorded, along with the form post
parameters. That way, when the test is run, the same data will automatically
be sent. Even the X and Y coordinates of where the button was clicked are
saved as part of the test, since these are also submitted as part of the
request.
4. Add additional steps to the test by logging out of the site and then re-
attempting the login with invalid credentials.
5. Once the desired tests have been recorded, close the browser windows and
save the test.
Automatically, the project will now include the Web test case file along with each of
the recorded requests.
Viewing a recorded Web test case
Selecting any of the nodes within the WebTest tree will allow you to modify the data
inside the Properties window
After recording a test we are ready to begin executing it. To execute all the tests
within a project, simply run the project. This will open up the Test Results windows
and mark each test as pending while it is in progress and Passed/Failed once
execution completes.
Request Rules
Extraction Rules
Extraction rules capture a response value so that at a later time the value can be
used within a request. Rather than parsing the entire HTTP response manually, the
extraction rules provide a means of focusing in on a particular data item within the
response. The extracted data item can then be validated or used again in a
subsequent post back. Our recorded example automatically added an extraction rule
when we logged on.
The rule is an ExtractHiddenFields rule whose data is posted back in the second
request of the Web test case. During the subsequent request, this data is submitted
back in a hidden field on the page. Other extraction rule options are
ExtractAttributeValue, ExtractHttpHeader, ExtractRegularExpression, &
ExtractText.
Validation Rules
Validation rules allow the test writer to examine the entire HTTP response and verify
that it is correct. For example, valid credentials should cause the welcome! message
to appear in the HTML response. A validation rule that checks for these items in the
response should be added. The validation rules verify that this text appears
somewhere within the response body.
If a particular rule fails, the request will be marked as failed, and the Details tab will
provide an explanation for what failed.
Both validation and extraction rules provide for text entry within the Properties
window of virtually every node. However, what makes Visual Studio Web Test
powerful is the fact that the text can be pulled from a database.
To test using a data source, click the Add Data Source button on the toolbar of the
Web test. In the ensuing dialog box, specify an OLE DB Provider as Microsoft OLE DB
Provider for SQL Server. Click on Data Links to enter the data source, authentication
method and database name.
Once a test has been configured with a data source, it is necessary to return to the
Edit Run Settings dialog box and change the run count to one run per data
source row. In this way, the test will repeat for each row in the newly configured
data source, and during each run, the parameters associated with the data source
will be assigned the value in the column for the particular row.
Generating Web Test Code
Included on the toolbar for a web test case is a Generate Code button. Clicking this
button prompts for a test name, and then generates a CS/VB file corresponding to
Web case. The generated code includes each validation and extraction rule that may
have been added.