[Solution] Unit Test series (1) Basics

Source: Internet
Author: User

Unit testing is not a new concept of software development, has been in existence in the 1970, has been proven to be one of the most desirable methods.

This series will be divided into 3 sections:

    1. Unit Testing Basics
    2. Break dependencies, use mock objects, pile objects, mock frames
    3. Create a good unit test

Index of this section:

    • Unit Testing and integration testing
    • Test-driven development
    • UnitTest and NUnit
    • First unit Test
    • Naming conventions

Unit Testing and integration testing

Unit testing is almost always written based on the framework, because the framework provides us with a unified API to manage testing.

Common frame with Unit Test (MS test), NUnit (open source)

Defined

A unit test is a piece of code that calls another piece of code and then tests some assumptions for correctness. (a cell refers to a method or function)

Integration testing refers to the testing of 2 or more interdependent software modules as a group.

Excellent unit Testing Guidelines

    1. Automatic, repeatable
    2. Easy to implement
    3. Continuously available
    4. Simple
    5. Fast

Test-driven (TDD) development

For the exact meaning of TDD, there are many different viewpoints, some people think that is the test first development, some people think that means a lot of testing, some people think is a design method.

The process of TDD:

Write test write code refactoring write next Test

It shows that TDD is incremental in nature, one small step at a time, and finally a high-quality software. (refactoring can be done after each test, or after several tests have been completed.) Refactoring is extremely valuable. )

Benefits of TDD:

    1. High code test coverage
    2. Testing is trustworthy
    3. Assisted design, reducing code complexity

MS Test and NUnit

All test frameworks share the same core features: Test Declaration, test execution, and assertions.

In. NET, you typically use attribute tags to add additional information, which is where Ms Test and NUnit differ on the Properties tab.

MS Test Attribute NUnit Attribute Use
[TestClass] [Testfixture] Define a test class that can contain many test functions and initialize, destroy functions (all of the following tags and other assertions).
[TestMethod] [Test] Define a separate test function.
[ClassInitialize] [Testfixturesetup] Defines a test class initialization function that, whenever one or more test functions in a test class is run, is called once before the test function is called (which is called before the first Test function is run).
[ClassCleanup] [Testfixtureteardown] Define a test class destroy function that runs whenever the selected test function in the test class finishes running (runs after the last Test function is run).
[TestInitialize] [SetUp] Defines the test function initialization function, which is called once before each test function is run.
[TestCleanup] [TearDown] Defines the test function destroy function, which is called once after each test function is executed.
[AssemblyInitialize] -- Defines the test assembly initialization function, which is called once before the test function in the assembly is run (it is called before the first Test function in assembly is run).
[AssemblyCleanup] -- Defines the test assembly destroy function, which runs once after all the test functions in the assembly run are finished. (called after all test functions in assembly have finished running)
[DescriptionAttribute] [Category] Defines the identity grouping.

First unit Test

Installation

For Ms Test, install vs will be installed automatically. In the toolbar = = Test = = Window = = Test Explorer opens.

For NUnit, click on the link to download the installation.

Coding

    1. Configuration objects
    2. Manipulating objects
    3. Assertion result
[TestClass] Public classdbcontexttests { PublicDbContext Db {Get;Set; } /// <summary>        ///each test method executes before execution/// </summary>[TestInitialize] Public voidInit () {//1 Configuration ObjectsDb =NewDbContext (); } [TestMethod] Public voidTestadd () {varBlog =NewBlog {Title ="The art of unit testing", Content ="unit Testing is an art" }; //2 manipulating Objectsdb.add (blog); //3 Assertion ResultsAssert.istrue (blog. Id >0); }    }

Test

Naming conventions

SUT Kind SUT
Project Create a new "project under test". Tests's Test project
Class A class that tests at least one new "class name" to be tested for each class
Method

Create at least one method name for each method name test scenario "expected behavior" method

Or use the simple name of test "method name"

Note: SUT ("System under test") represents the system being tested, and some people like cut ("code under test"). usually SUT.

This article never, C

This article link: http://www.cnblogs.com/neverc/p/4742654.html

[Solution] Unit Test series (1) Basics

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.