Full access to some basic ideas and concepts in TDD-2. Unit Test and preliminary understanding of NUint

Source: Internet
Author: User
1. Unit Test: a small piece of code written by the developer to check whether a very small and clear function of the tested code is correct.
2. Specific unit test performance: used to determine the behavior of a specific function or method under a specific condition or scenario.
3. unit test Purpose: To prove that the behavior of a code segment is indeed consistent with the developer's expectation.
4. Core Content of unit testing: this simple and effective technology is designed to make code more perfect.
5. The static method of Assert class in NUint:
1) AreEqualsAssert. AreEqual (expected, actual [, string message])
// Expected: expected Value (usually hard-coded );
// Actual: the actual value of the tested code;
// Message: an optional message that will be reported when an error occurs.

Because the computer cannot accurately represent all floating point numbers, when comparing floating point numbers (float or double), you must specify an additional error parameter. Assert. AreEqual (expected, actual, tolerance [, string message])
// Tolerance: The specified error, that is, it is enough to be accurate to the X-digit after the decimal point.
// For example, the value is precise to the fourth digit after the decimal point.
Assert. AreEqual (0.6667, 0.0/3.0, 0.0001 );

2) IsNullAssert. IsNull (object [, string message])
// Null
Assert. IsNotNull (object [, string message])
// Non-null

3) AreSameAssert. AreSame (expected, actual [, string message])
// Verify whether the expected and actual parameters reference the same object.

4) IsTrueAssert. IsTrue (bool condition [, string message])
Assert. IsFalse (bool condition [, string message])

5) FailAssert. Fail ([string message])
// Make the test fail immediately. This type of assertion is usually used to mark a branch that should not be reached, but is not commonly used.

6. When a test fails, you cannot add any new features to the original code.
7. NUnit framework
The minimum requirement for using the NUint Framework is 1 using NUint. Framework;
2 [TestFixture]
3 public class TestSimple
4 {
5 [Test]
6 public void TestMethod ()
7 {
8 Assert. AreEqual (2, 4/2 );
9}
10}

1) The NUint. Framework namespace is required. NUint. dll must be referenced in the project;
2) each class containing the test must be marked with the TestFixture attribute, and the class must be public.
3) All public methods marked with the Test attribute in the Test class will be automatically executed by NUint.

8. Category of NUint Categories
The Category concept provides a simple way to mark and run individual tests and TestFixture.
A Category is a name defined by yourself. You can associate different test methods with one or more Category, and select the desired Category when running the test.
For example, in practice, some tests can be completed in just a few seconds, while others can be completed in a long time. To avoid long-running tests, you can use classification to mark them, then run the test to specify the Category to be run.

1 [Test]
2 [Category ("Short")]
3 public void upload test ()
4 {
5 // do some tests.
6}
7 [Test, Category ("Long")] // you can write the two attributes.
8 public void LondTest ()
9 {
10 // do some tests.
11}

Similarly, sometimes it is required that some Category tests can be run if no Category is explicitly selected, and when some Category is selected, only the selected Category is executed.
You need to set the Explicit in the Category attribute to true.
For example: [Category ("Special", Explicit = true)]

If no Category is specified in the running GUI, the preceding settings will prevent the Category from being tested.

By default, when a method does not specify any Category, it is equivalent to not displaying the specified Explicit (that is, Explicit = false), and such methods will be executed.

In addition to the method, you can set Category. For example, if TestFixture has been tested for a Long time, you can set it to [Category "Long")]. in this way, there is no need to repeat each test method.

9. Each test should run independently of each other. Each test can be run in any order at any time.
10. In NUint, Per-method: Setup and Teardown [SetUp] // used to establish the test environment
Public void MySetup (){
// Do something.
}

[TearDown] // clear the test environment
Public void MyTeardown (){
// Do something.
}

Before each [Test] method, the [SetUp] method is called. After each [Test] method is completed, the [TearDown] method is called.

11. Per-class: TestFixtureSetUp and TestFixtureTearDown in NUint
These two methods set some environments for the entire Test Class and clean up after all the Test methods are executed.
Before the first [Test] method is run, the [TestFixtureSetUp] method is called. After all [Test] methods are executed, the [TestFixtureTearDown] method is called.

12. Test the expected exceptions. // For example, test whether ArgumentException is thrown.
[Test, ExpectedException (typeof (ArgumentException)]
Public void TestForException ()
{
Do. GetSomething (null );
// Shouldn's get to here;
}

If the test method throws an ArgumentException exception as expected, the test will pass. If no exception is thrown, the test will fail.
Once an exception is thrown, the remaining code in the test method will not be executed.

13. Ignore the specified Test method [Test, Ignore ("Not run this method")]
Public void TestMethod ()
{
// Do something
}

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.