Implemented using xUnits unit test

Foreword

From start to knock the code now, I am kept on shouting remember to do the test, remember self-test, test personnel to fight back buckle your money and the like, since the beginning of distressed money (of course, for code quality), on the old honest finished it their own to run their own again, no process is not planning no test documentation is to organize their own set of data to run again, and finally let testers are still a lot of test questions honestly to throw you.

unit test

First of all, or to talk about why it wants to test.

  1. Contribute to the overall robustness test the code coverage testing, stress testing is to more comprehensive perspective of better and faster customer service.
  2. A means test helps to improve motivation and cause their program ape attention, after all, a pit planted once was enough, two can tolerate, Zaisanzaisi five more afraid to be engaged, and this was also self-improvement it.
  3. Work finishing the software development process is to test, around, but, after all, acceptance is the ultimate goal, to achieve their due.

Well, after talking with them, of course, I am not a professional testers, certainly not to a test document templates, Well, according to this specification up, I always wanted to try mainly to tinker with my previous unit testing, this automation one of the means test, and always wanted to try but have been placed.

In MSTest , NUnit , xUint of these three I hesitated a little, but Sanqi twenty-eight he does, just to a bar, we chose the xUnit , MSTest is of course the official, support should be a little high, but this is not the choice of where we hesitate.

xUnit

First, we create a new project April.Test .
New

Fact

After the New, we see a default  [Fact] .

This test is a standard format, and if we do not need test data, then, is the method under the label do assert, simply give you an example.

        [Fact]
        public void TestEqual() { int a = 10, b = 20; Assert.Equal(30, Add(a, b)); } private int Add(int a, int b) { return a + b; }

After Qiaoxia Assert, assert found a lot of methods, not setting them up here, I feel all came from the name of the method to understand, there is not too much description, and also some testing example code, address given at the end of the text.

Theory

We can see in the above code, all data are given their own good, if I want to test how their own argument to do it, this time we should use  [Theory]  label and  [InlineData]  , the label is in order to better distinguish the type of method (personal understanding), test to see how this argument passed to engage in it.

        [Theory]
        [InlineData(new object[] { 1, 2, 3, 4 },1)] [InlineData(new object[] { "t", "e", "s", "t" }, "t")] public void TestContains(object[] objs,object obj) { Assert.Contains(obj, objs); }

Of course, we can also use a custom arrays to do the test data source, and here I initially thought I could pass any type of argument, but MemberData only supports object [].

        [Theory]
        [MemberData(nameof(tempDatas))]
        public void TestData(int a, int b) { int result = a + b; Assert.True(result == Add(a, b)); } public static IEnumerable<object[]> tempDatas { get { yield return new object[] { 1, 2 }; yield return new object[] { 5, 7 }; yield return new object[] { 12, 12 }; } }

Controller

In the time before tinker with unit tests, I have been thinking about a problem, but if such an operation, then what is the significance test, but later discovered that unit testing can do a lot more than I thought, so that learning is a non-stop trip, walk more, also more scenic.

Here you can introduce a process in our own test engineering, we need to finish the single three-step test.

  • Arrange (preparation)
  • Act (Implementation)
  • Assert (assertion results)

Before the test, we need to introduce a current project Moq , as to why do with this, just as we need to simulate vue (although mock with Moq is not so similar), the total can not test unit we introduced orm to actually operate the database bar (of course, this test library is also possible), so we need to implement an interface class and method of simulation.

Introduced

The introduction of finished, we will April.WebApi into the current project, then we create a class interface testing Values of ValuesControllerTest , then tested as follows:

        [Fact]
        public void TestGet() { // Arrange var mockRepo = new Mock<IStudentService>(); var controller = new ValuesController(mockRepo.Object); // Act var result = controller.Get(); // Assert Assert.Equal(new string[] { "value1", "" }, result.Value); }

Note here, the interface methods corresponding to the comment I have is, ultimately only returns an array of strings, because the method requires a corresponding interface to initialize, then get interface data of our test, look at the usage of this Moq it, first we need to implement the corresponding interface Mock initialize, and then we implemented to get the data corresponding to the analog method.

        [Theory]
        [InlineData(1)]
        public void TestGetByID(int id) { var mockRepo = new Mock<IStudentService>(); mockRepo.Setup(repo => repo.GetList(s => s.ID == 38).ToList()) .Returns(GetList()); var controller = new ValuesController(mockRepo.Object); var result = controller.Get(id); Assert.NotNull(result); Assert.Contains("大洛阳", result.Value); } private List<StudentEntity> GetList() { List<StudentEntity> entities = new List<StudentEntity>(); entities.Add(new StudentEntity() { ID = 1, Name = "小明", Number = "123456", Age = 19, Sex = 1, Address = "大洛阳" }); entities.Add(new StudentEntity() { ID = 2, Name = "小红", Number = "456789", Age = 18, Sex = 0, Address = "大洛阳" }); return entities; }

Write this I found that I was introduced WebApi time, seems to have put a few projects have been introduced in, this does not know the right fit.

summary

I write to you, basically the unit test this child also easy to go again, as to how they implement in your business, or thinking requires a combination of a small program to practice, the thing to go through a demo only feasible explanation, go through different body works in order to explain the amount of available, including follow-up integration testing, stress testing, automated testing will start to tinker a little something, and long way ah.

 

Guess you like

Origin www.cnblogs.com/cider/p/11825015.html