This article will discuss the basic of Unit Test.
Unit tests give developers and testers a quick way to look for logic errors in the methods of classes. The content of this article:
Unit testing is a software testing method by which individual units of source code—sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures—are tested to determine whether they are fit for use.
Image, when we have a developed software, QA spent months to test it throughly, while in the last stage, we find a new bug, it is a very simple fix, however, to make sure the software working well in the whole, we need to test it piece by piece, so called regression testing. If let QA to retake the job, that will take one testing cycle, months work. However, if we have s unit test for all individual piece of software, especially, if the tests can be run automatically, then we can reduce the time for the regression testing. This is what the unit test for.
- A - Introduction
- B - AAA in Unit Test
- C - Testing tools
- D - A Sample of MS Unit Test
- E - Why Unit Test
- F - Characteristics of a good unit test
B - AAA in Unit Test
The AAA (Arrange, Act, Assert) pattern is a common way of writing unit tests for a method under test.
-
The Arrange section of a unit test method initializes objects and sets the value of the data that is passed to the method under test.
-
The Act section invokes the method under test with the arranged parameters.
-
The Assert section verifies that the action of the method under test behaves as expected. For .NET, methods in the Assert class are often used for verification.Unit test tools and tasks --- MS
C - Testing tools
.NET is a multi-language development platform, for each language, one can choose between several test frameworks.
C -1: NUnit
NUnit is a unit-testing framework for all .NET languages. Initially, NUnit was ported from JUnit (born on 1997 on a flight by Kent Beck, Erich Gamma), and the current production release has been rewritten with many new features and support for a wide range of .NET platforms. It's a project of the .NET Foundation.
Major atributes:
[TestFixture]
--- a class that contains unit tests.
[Test]
--- a method is a test method.
C -2: xUnit
xUnit is a free, open-source, community-focused unit testing tool for .NET. The original inventor of NUnit v2 wrote xUnit.net. xUnit.net is the latest technology for unit testing .NET apps. xUnit.net is a project of the .NET Foundation and operates under its code of conduct.
Major atributes:
none
--- a class that contains unit tests.
[Fact]
--- a method is a test method.
C - 3: MSTest
MSTest is the default test framework that is shipped along with Visual Studio. It is the Microsoft test framework for all .NET languages. It's extensible and works with both .NET CLI and Visual Studio.
Major atributes:
[Testclass]
--- a class that contains unit tests.
[TestMethod]
--- a method is a test method.