TDD (test-driven development) learning two: Creating a first TDD program

Source: Internet
Author: User

In this section we will learn about the building of test-driven development environments, test-driven development concepts and processes. All of the content involved will be posted in the form, if you are also interested, you can follow the steps, if you have any questions, you can leave a message, I will be happy to answer your questions.

Preparation Tools

Visual Studio (whatever version doesn't matter), NUnit

Installing NUnit

Download the latest version of NUnit from NUnit official website, the address is http://www.nunit.org/index.php?p=download, download this msi, then install it and get it done directly next.

In the project reference NUnit

first , create a new project in VS = = Add Project = = In the template select other Project type = = "vs Solution = =" Blank solution, and then named MYFIRSTTDD, for example.

then, on top of the new solution, create a new project and add a class library Tdd.TicTacToe.Model.

after the add is complete , continue adding the Tdd.TicTacToe.ModelTests class library.

Right-click on the solution, "open folder in File Explorer" appears, after opening the folder, create a new folder in the folder, named Lib. Then from the folder that installs NUnit from the beginning, put Nunit.framework.dll (find the directory where you just installed NUnit, mine is in C:\Program Files (x86) \nunit 2.6.4\bin\framework folder) , copy to the new Lib folder you just created.

continue to add = = in the VS right-click = "New Solution folder, named Lib, add the existing item in the Lib Solution folder and add the Nunit.framework.dll in the Lib folder."

in the Tdd.TicTacToe.ModelTests the reference above right-click, add just Unit.framework.dll and reference to Tdd.TicTacToe.Model.

OK, to this step, the entire NUnit reference has been completed, if your solution is the same as mine, then congratulations, you can go to the next step, if not the same, please check your steps, what questions can leave a message.

Basic knowledge of TDD

Now, before we use TDD, we understand some of the fundamentals and processes of TDD.

3 a template

3 A template similar to the PDCA management idea in management, before doing something, prepare the test environment (Arrange), execute the method of the prepared test (ACT), and compare the results of the test and the original plan to see if it is consistent (Assert).

Assert static method

I remember when I read C # books, I saw the word assert, but never users, and now you finally have the opportunity to use the word, and it has a lot of static methods, we first understand.

Test-driven development process

"The Analects of Confucius and learning and" there is such a sentence, "My day provinces, for the conspiracy and infidelity?" With a friend instead of a letter? Do not pass? ", plain English is" I have many times every day to reflect on their own words and deeds, for others to plan the matter is not dedicated to it? Isn't it good to be with friends? Is there a reason to teach someone else to do that? "And for TDD, you have to think about how to make your program wrong and whether it's better to think about it every time you test."

Write the first TDD test program

Add a class in Tdd.TicTacToe.ModelTests, name it simpletest, and add the following code definition.

After saving, right-click on the Tdd.TicTacToe.ModelTests, and then start the menu to open the newly installed NUnit, file = = "Open Project, Find the compiled Tdd.TicTacToe.ModelTests.dll, mine is under the Myfirsttdd\tdd.tictactoe.modeltests\bin\debug folder. After loading, run it, then you will see the Red Fork, the test did not pass, you know why not.

Look at the Tips

Tdd.TicTacToe.ModelTests.SimpleTest.My_First_NUnit_Test:
Expected:3
But Was:4

The result is expected to be 3, the actual result is 4, so this time we go back to modify our program, the 3+1 changed to 2+1, or changed to 1+2,3+0, regenerate, re-run, congratulations to you, into the whole green, success.

Write these today, this series I will continue to write, next, I will use TDD to complete the development of a complete small game, so that we can understand the TDD development model more deeply, please look forward to, if you think I wrote this article well, please click the recommended button recommended.

TDD (test-driven development) learning two: Creating a first TDD program

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.