Test-driven development practices-entry

Source: Internet
Author: User
In this example TestThe framework is nunit. You can download this tool at http://www.nunit.org.

The nunit. Framework. dll in nunit must be referenced in the test project.

Let's start with a simple requirement.

 Requirement

The employee logs on to the system, enters the login name and password, and the system returns whether the login is successful.

First, we need to write test cases for this requirement:

1 [testfixture]
2 public class employeetest
3 {
4 [test]
5 Public void logintest ()
6 {
7. Employee em = new employee ();
8 assert. istrue (Em. login ("admin", "admin"), "User Login test failed ");
9}
10}

This code is not compiled here. First, we need to create an employee class and create the login (string loginname, string password) method:

1/** // <summary>
2 // employees
3 /// </Summary>
4 public class employee
5 {
6/** // <summary>
7 // employee login
8 /// </Summary>
9 /// <Param name = "loginname"> logon name </param>
10 /// <Param name = "password"> password </param>
11 /// <returns> </returns>
12 public bool login (string loginname, string password)
13 {
14 return false;
15}
16}

Then we compile the code, but the test turns on the red light and the test fails. Next we will implement this method:

1/** // <summary>
2 // employees
3 /// </Summary>
4 public class employee
5 {
6/** // <summary>
7 // employee login
8 /// </Summary>
9 /// <Param name = "loginname"> logon name </param>
10 /// <Param name = "password"> password </param>
11 /// <returns> </returns>
12 public bool login (string loginname, string password)
13 {
14 if (loginname = "admin" & Password = "admin ")
15 {
16 return true;
17}
18 return false;
19}
20}

Then let's test and pass.

At this point, our basic functions have been tested successfully, but we also need to add a verification code for the blank username. We should first write a test case like above:

1 [test]
2 [expectedexception (typeof (argumentexception), expectedmessage = "login name cannot be blank", usermessage = "user verification test failed")]
3 Public void validateloginname ()
4 {
5 employee em = new employee ();
6 em. login (null, null );
7}

This Code fails to be tested here, because we have not implemented it. Next we will implement this judgment. The final modification of the Code is as follows:

1/** // <summary>
2 // employee login
3 /// </Summary>
4 /// <Param name = "loginname"> logon name </param>
5 // <Param name = "password"> password </param>
6 /// <returns> </returns>
7 public bool login (string loginname, string password)
8 {
9 If (string. isnullorempty (loginname ))
10 {
11 throw new argumentexception ("the login name cannot be blank ");
12}
13 if (loginname = "admin" & Password = "admin ")
14 {
15 return true;
16}
17 return false;
18}

Then we can test it in the nunit tool. It's a green light.

We have completed the basic functions. In the next section, we will explain how to refactor the code ......

From: http://www.51testing.com/html/79/n-132679.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.