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:
- Unit Testing Basics
- Break dependencies, use mock objects, pile objects, mock frames
- 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
- Automatic, repeatable
- Easy to implement
- Continuously available
- Simple
- 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:
- High code test coverage
- Testing is trustworthy
- 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
- Configuration objects
- Manipulating objects
- 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