Assert class in unit test and Assert class in unit test
I. Use of Assert class
1. The namespace of the Assert class is Microsoft. VisualStudio. TestTools. UnitTesting. You only need to reference Microsoft. VisualStudio. QualityTools. UnitTestFramework. dll in the project file.
2. The Assert class can be used to verify specific functions. The unit test method executes the method code in the development code, but only when the Assert statement is included can the code behavior be reported.
3. In the test method, Assert can call any number of Assert class methods, such as Assert. AreEqual. The Assert class has many methods to choose from, and many of them have multiple overloading.
4. You can use the CollectionAssert class to compare an object set or verify the status of one or more sets.
5. Use the StringAssert class to compare strings. This class contains various useful methods. For example, StringAssert. Contains, StringAssert. Matches, and StringAssert. StartWith.
6. AssertFailedException if the test fails, an AssertFailedException exception is thrown. If the test times out, an unexpected exception is thrown, or an Assert statement containing the Failed result is generated, the test fails.
7. AssertInconclusiveException as long as the test result is Inconclusive, AssertInconclusiveException is thrown. Generally, add Assert. Inconclusive to a test that is still being processed to indicate that the test is not ready and cannot be run.
Ii. Main static members of the Assert class
1. AreEqual: The method is overloaded for N multiple times. The main function is to determine whether the two values are equal. If the two values are not equal, the test fails. 2. AreNotEqual: The method is overloaded for N multiple times. The main function is to determine whether the two values are not equal. If the two values are equal, the test fails. 3. AreNotSame: whether the referenced objects are different. If the two inputs reference the same objects, the test fails. 4. AreSame: whether the referenced objects are the same. If the two inputs reference different objects, the test fails. 5. Fail: The assertion fails. 6. Inconclusive: test results that cannot be proved to be true or false. 7. IsFalse: whether the specified condition is false. If the condition is true, the test fails. 8. IsTrue: whether the specified condition is true. If the condition is false, the test fails. 9. IsInstanceofType: whether the specified object is an instance of the required type; if the required instance is not in the inheritance hierarchy of the object, test loss 10, IsNotInstanceofType: test whether the specified object is an instance of the required type; if the required instance is in the inheritance hierarchy of the object, the test fails. 11. IsNull: test whether the specified object is non-empty. 12. IsNotNull: test whether the specified object is non-empty.
Iii. test instancesC # code Replication
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.Collections.Generic;
namespace Temp_Test
{
/// <summary>
/// This is the test class for TempDll.TempDll, designed to
/// include all TempDll.TempDll unit tests
/// </ summary>
[TestClass ()]
public class TempDllTest
{
private TestContext testContextInstance;
/// <summary>
/// Get or set the test context, the context provides
/// Information about the current test run and its functions.
/// </ summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
/// <summary>
/// At the entrance of the test method
/// </ summary>
[TestMethod ()]
public void Test ()
{
System.IO.FileInfo file = null;
System.IO.FileInfo file1 = file;
System.IO.DirectoryInfo dir = null;
string tempTrue = "T";
string tempFalse = "F";
Assert.AreEqual (tempTrue, tempFalse); // The two pairs are not the same, the test failed
Assert.AreNotSame (file1, tempTrue); // Two imported objects are the same, the test failed "
Assert.AreSame (file1, file); // The two imported objects are not the same
Assert.Fail (); // Whatever it is, declare test failure directly
Assert.Inconclusive (); // Same as Fail, but not failed, but the test fails
Assert.IsFalse (true); // If true, the test failed
Assert.IsTrue (false); // If false, please indicate that the test failed
Assert.IsInstanceOfType (true, tempTrue.GetType ()); // The types are different, the test failed
Assert.IsNull (tempFalse); // Not empty so the test failed
}
}
}
Is the java Assert class used for unit testing outdated? What should I use now?
How is the Assert out of date? During unit testing, junit4 is often used with Assert.
In VS2010 unit test, what are the major advantages of Assert assertions? How to write code for testing? New guy on the road, advice from my brother and sister
The basic method of unit test is to call the function of the tested code, enter the parameter value of the function, obtain the returned result, and then compare it with the expected test result. If it is equal, the test is passed, otherwise, the test fails.
1. Use of Assert class
Assert. Inconclusive () indicates an unverified test;
Assert. AreEqual () test whether the specified value is equal. If the value is equal, the test passes;
AreSame () is used to verify that the specified two object variables point to the same object. Otherwise, it is considered as an error.
AreNotSame () is used to verify that the specified two object variables point to different objects. Otherwise, it is considered as an error.
Assert. IsTrue () test whether the specified condition is True. If it is True, the test passes;
Assert. IsFalse () test whether the specified condition is False. If it is False, the test passes;
Assert. IsNull () test whether the specified object is a null reference. If it is null, the test passes;
Assert. IsNotNull () test whether the specified object is not empty. If not, the test passes;
2. Use of the CollectionAssert class
Used to verify whether the object set meets the conditions
Use of the StringAssert class
Used to compare strings.
StringAssert. Contains
StringAssert. Matches
StringAssert.tar tWith
Reference: zxianf.blog.163.com/...57499/