In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "what are the methods of .NET Core unit testing". The editor shows you the operation process through an actual case, which is simple, fast and practical. I hope this article "what are the methods of .NET Core unit testing" can help you solve the problem.
There are many types of application testing, including integration testing, Web testing, load testing and so on. At the bottom is the unit test, which can test a single software component or method. Unit tests generally test only the developer's code, not infrastructure, problems, such as databases, file systems, or network resources. Unit tests can be written using test-driven development (TDD), or they can be added to existing code to confirm their correctness.
When writing unit tests, be careful not to casually introduce dependencies into the infrastructure. These dependencies tend to make tests slower and more fragile, so they should be retained for inheritance tests. You can avoid these hidden dependencies in your application code by using dependency injection to request dependencies from the framework by following the principle of showing dependencies.
1.xUnit.net unit test
Create a. Net core class library project and create a method for testing:
Public class UnitTest {public bool IsNum (string obj) {var I = 0; return int.TryParse (obj, out I);}}
Then add the xUnit test project:
Create methods for test success and test failure:
Public class UnitTest1 {private UnitTest myTest; public UnitTest1 () {myTest = new UnitTest ();} [Fact] public void TestSuccess () {Assert.True (myTest.IsNum ("1"));} [Fact] public void TestFail () {Assert.True (myTest.IsNum ("Q")) }}
The [Fact] feature represents a method as a single test.
Select Test-run-all tests in the VS toolbar to show one success and one failure:
You can also use the [Theory] feature to execute the same code, but for tests with different input parameters, use the [InlineData] attribute to specify the parameters for these inputs:
[Theory] [InlineData ("- 1")] [InlineData ("0")] public void Test (string str) {Assert.True (myTest.IsNum (str));} 2.MSTest unit test
XUnit makes it a good choice for cross-platforms, and MSTest is a better choice for Windows, because it is officially supported.
Create a MSTest test project, also add the test success and failure methods, add the [TestMethod] attribute to the method, and add the feature [TestClass] to the class:
[TestClass] public class UnitTest1 {private UnitTest myTest; public UnitTest1 () {myTest = new UnitTest ();} [TestMethod] public void TestSuccess () {Assert.IsTrue (myTest.IsNum ("1")) } [TestMethod] public void TestFail () {Assert.IsTrue (myTest.IsNum ("Q"));}} this is the end of the introduction on "what are the methods of .NET Core unit testing". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.