Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use VisualStudio for unit testing

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use VisualStudio for unit testing, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

1. Prepare to test the code. The code to be tested this time is the same as in the previous article. Namespace BigMan.UnitTest {public class Program {public static int Add (int a, int b) {return a + b;} public static int Div (int a, int b) {return a / b;} static void Main (string [] args) {}} .csharpcode, .csharpcode pre {font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace Background-color: # ffffff; / * white-space: pre;*/}. Csharpcode pre {margin: 0em;} .csharpcode .rem {color: # 008000;} .csharpcode .kwrd {color: # 0000ff.} .csharpcode .str {color: # 006080;} .csharpcode .op {color: # 0000c0;} .csharpcode .preproc {color: # cc6633;}. Csharpcode .asp {background-color: # ffff00;}. Csharpcode .html {color: # 800000 } .csharpcode .attr {color: # ff0000;} .csharpcode .alt {background-color: # f4f4f4; 100%; margin: 0em;} .csharpcode .lnum {color: # 606060;} 2. Since it is data-driven, you also need to prepare the data for testing.

There is no specific requirement for the type of data source, so it should be OK as long as it is a data source type supported by .NET. The data source type used in this demonstration is Access, as shown in the following figure. A table, AddMethod, is used to hold the test data of the Add method, and the columns in the table correspond to the two parameters of the Add method and the return values.

3. With the data, the next step is to configure the data source into the test program

The binding of the data source is realized through a DataSourceAttribute feature, and the specific method is also very simple. On the method name of the test method, use this DataSource, and pass in the connection string and the data table name.

[TestMethod ()] [DataSource (@ "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\ Users\ bigman\ Documents\ TestData.accdb", "AddMethod")] public void AddTest () {.}

The constructor for this feature also has an overloaded DataSourceAttribute (String) that takes only one parameter, which corresponds to the key value in the configuration file, that is, saving the connection string and table name in app.config.

4. Write test code / [TestMethod ()] [DataSource (@ "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\ Users\ bigman\ Documents\ TestData.accdb", "AddMethod")] public void AddTest () {/ / Access the dataint x = Convert.ToInt32 (TestContext.DataRow ["FirstNumber"]); int y = Convert.ToInt32 (TestContext.DataRow ["SecondNumber"]) Int expected = Convert.ToInt32 (TestContext.DataRow ["Sum"]); int actual = Program.Add (x, y); Assert.AreEqual (expected, actual, "x: y:", new object [] {x, y}); .csharpcode, .csharpcode pre {font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: # ffffff / * white-space: pre;*/}. Csharpcode pre {margin: 0em;} .csharpcode .rem {color: # 008000;} .csharpcode .kwrd {color: # 0000ff;} .csharpcode .str {color: # 006080;} .csharpcode .op {color: # 0000c0;} .csharpcode .preproc {color: # cc6633;} .csharpcode .asp {background-color: # ffff00;} .csharpcode .html {color: # 800000;} .csharpcode .attr {color: # ff0000 } .csharpcode .alt {background-color: # f4f4f4; 100%; margin: 0mm;} .csharpcode {color: # 606060;} .csharpcode, .csharpcode pre {font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: # ffffff; / * white-space: pre;*/} .csharpcode pre {margin: 0em } .csharpcode .rem {color: # 008000;} .csharpcode .kwrd {color: # 0000ff;} .csharpcode .str {color: # 006080;} .csharpcode .op {color: # 0000c0;} .csharpcode .preproc {color: # cc6633;} .csharpcode .asp {background-color: # ffff00;} .csharpcode .html {color: # 800000;} .csharpcode .attr {color: # ff0000;} .csharpcode .alt {background-color: # f4f4f4; 100% Margin: 0mm;} .csharpcode .lnum {color: # 606060;} .csharpcode, .csharpcode pre {font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: # ffffff; / * white-space: pre;*/}. Csharpcode pre {margin: 0em;} .csharpcode .rem {color: # 008000;} .csharpcode .kwrd {color: # 0000ff } .csharpcode .str {color: # 006080;} .csharpcode .op {color: # 0000c0;} .csharpcode .preproc {color: # cc6633;} .csharpcode .asp {background-color: # ffff00;} .csharpcode .html {color: # 800000;} .csharpcode .attr {color: # ff0000;} .csharpcode .alt {background-color: # f4f4f4; 100%; margin: 0em;} .csharpcode .lnum {color: # 606060;}

In the above code, you can see a key attribute, that is, the TestContext,DataRow we mentioned earlier is of type System.Data.DataRow, and "FirstName" is the column name in the corresponding data source. As long as you are familiar with this use of TestContext, the code is as simple as ever.

5. Run the test

Thank you for reading this article carefully. I hope the article "how to use VisualStudio for unit testing" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report