Explain. Net unit test method in detail

Time:2019-11-27

1. Abnormal test

It can be used to test method exceptions directly or to test simulation objects. However, it is rarely used to test simulation objects. Therefore, this paper introduces the method exception test. Please see the following code. When the user name is empty, an exception will be thrown.

for example


 
public bool Valid(string userName, string passWord)
 
  {
 
    if (string.IsNullOrEmpty(userName)) throw new ArgumentNullException("userName is null");
 
    var isValid = userName == "admin" && passWord == "123456";
 
    Log.Write(userName);
 
    return isValid;
 
  }

The test code is as follows


 
[Test]
 
 [ExpectedException(typeof(ArgumentNullException))]
 public void Vaild_Throw_Test()
 {
   MyLogin l = new MyLogin();
   l.Valid("", "123456");
 }

2. Test return value

A class of lastcall is used here, which is quite common. Some auxiliary testing functions are in this class.

The test code is as follows


[Test]
public void Valid_Return()
{
  MockRepository mock = new MockRepository();
  var log = mock.DynamicMock<ILog>();
  using (mock.Record())
  {
    log.WriteLog("admin");
    LastCall.Return(0);
  }
  var returnValue = log.WriteLog("admin");
  Assert.AreEqual(returnValue, 0);
}   

Recommended Today

Zookeeper’s cursor

I talked about the use of zookeeper’s API and the use of zookeeper’s zkclient. Now let’s take a look at the use of cursor. Maven dependence <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.2.0</version> </dependency> Create session Before, for creating a session, this part of code will not be pasted later CuratorFramework client; @Before public void before() { client […]