One of the DotNet Development Tools Series NUNIT

xiaoxiao2021-03-06  45

I often see that everyone has mentioned NUNIT unit test tool. I think this tool is very strong. But I will introduce it to the introduction of it. The information is too small, but put it out of a list of it, just started using it. I don't want to share my mind. I am here to share my learning process. I hope to help my colleagues who want to learn this knowledge. First find this software installation, this everyone will go, go Google can find it. Open vs development Environment. Creating a new class. Here I also talked about the train tickets that you usually say: use the NUNIT.FRAMEWORK file. Using system; using nunit.framework

Summary description of Namespace NUNITEST {///

/// Ticket. /// public class ticket {private int _amount;

Public Ticket () {// // Todo: Add to the program code for the constructive function here //}

Public int Amount {get {return_amount;}}

Public void sell () {if (_AMOUNT-1 <0) throw new exception ("no"); _AMOUNT - = 1;} public void add (int Num) {_AMount = Num;

}}} The top is a class regarding the train ticket. There are two ways, one is add. One is a Sell. These two methods are the roles of these methods. The add to the train ticket. Sell is the train ticket. In call add We must make it increase in the number. When selling, let's reduce the train ticket. Our logic is reflected. But now we have to know if it is really correct, if it is not a test In the case, we have to create a new instance to verify it, this invisible increases our workload. So we think of using NUNIT's unit test tool to test whether it is correct. Now let's write a test class to verify: [TestFixTure ] Public class tickettest {public tickettest () {}

Private tickett;

[TestFixTureTuP]

Public voidin () {t = new ticket (); T.Add (3);

[Test] public void add () {ask.Areequal (3, t.amount);

[Ignore ("Ignore this void")] public void sell () {t.sell (); assert.arequal (0, t.amount);} [test]] public void ExcPetationTesting ) {T.sell (); t.sell (); t.sell (); t.sell (); assert.Arequal (0, t.amount);}} After compiling, open NUnit interface as follows:

Open the DLL file just compiled through the File menu. You can see the following results in pressing Run. Green will have a problem, if it is red, it means that this class is written. You can change some Dongdong and then test it. See if it is correct. Welcome everyone to throw bricks, I hope to help the brothers who want to start with this stuff [TestFixTure] This class is a test class. That is to say, if we want to test a certain class, This property is added above this class. This attribute is to reference NUnit.framework. [Test] means this method is a test method. [Ignore ("Ignore (" Ignore this void ")] means ignoring this method, not Test [TestFixTureTUP] means running this method when you start testing this class. It is also possible to say that it is the initialization of some classes [TestFixTureTeartown] to run this method when the test is completed. You can do some resource releases. [ SETUP] Indicates that this method is run before each test method [Teardown] means that this method is running at each test method. [TEST] [eXpectedException] These two properties are used together The test has a normal exception trigger. The above is a few properties of several NUnit. These are before they are tested or have to run the way. The way is used is assert.Arequal. This is used. Compare two objects or values, there are some other methods, here to speak these stuffs first, welcome everyone to discuss all the source code for this test class Using system; use nunit.framework; namespace NUnitTest {/ //

/// Ticket summary description. /// public class ticket {private int _amount;

Public Ticket () {// // Todo: Add to the program code for the constructive function here //}

Public int Amount {get {return_amount;}}

Public void sell () {if (_AMOUNT-1 <0) throw new exception ("no"); _AMOUNT - = 1;} public void add (int Num) {_AMount = Num;

}} [TestFixTure] public class tickettest {public tickettest () {}

Public Ticket T;

[TestFixTureTuP]

Public voidin () {t = new ticket (); T.Add (3);

[Test] public void add () {ask.Areequal (3, t.amount);

[Ignore ("Ignore this void")] public void sell () {t.sell (); assert.arequal (0, t.amount);} [test]] public void ExcPetationTesting ) {T.sell (); t.sell (); t.sell (); t.sell (); assert.arequal (0, t.amount);}}}

转载请注明原文地址:https://www.9cbs.com/read-65260.html

New Post(0)