zoukankan      html  css  js  c++  java
  • 单元测试特性标签(结合代码)

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    namespace UnitTestProject
    {
        /// <summary>
        /// 特性标签:
        /// [TestClass]用来标识一个包含自动化测试的类
        /// [TestMethod]可以放在一个方法上,标识这是一个需要被调用的自动化测试。
        /// [ExpectedException]测试异常,有助于确保代码能够在必须抛出异常时抛出异常。
        /// [Ignore]忽略测试
        /// [TestCategory]设置测试类别(特征)测试类别有助于我们以符合逻辑的方式来对测试进行分组。
        /// </summary>
        [TestClass]
        public class UnitTest1
        {
            LogAnalyzer analyzer = new LogAnalyzer();
    
            [TestMethod]
            [ExpectedException(typeof(ArgumentException),"测试失败")]
            public void TestMethod1()
            {
                analyzer.IsValid(string.Empty);
                //Assert.IsTrue(true, "测试失败");
                Assert.AreSame(int.Parse("0"), 00);
            }
            [TestMethod]
            [Ignore]
            public void TestMethod2()
            {
                analyzer.IsValid(string.Empty);
            }
            [TestMethod]
            [TestCategory("运行快的测试")]
            public void TestMethod3()
            {
    
            }
            [TestMethod]
            [TestCategory("运行快的测试")]
            public void TestMethod4()
            {
    
            }
            [TestMethod]
            [TestCategory("运行慢的测试")]
            public void TestMethod5()
            {
    
            }
        }
        /// <summary>
        /// 使用以下两个特性来确保所有测试都使用新的未更改的状态。
        /// [TestInitialize]标识在测试之前要运行的方法,从而分配并配置测试类中的所有测试所需的资源。
        /// [TestCleanup]标识一个方法,此方法包含测试运行后必须用于释放测试类中的全部测试所获得的资源的代码。
       /// 注意点:
    /// 通常的做法是为每个被测类建立一个测试类,为每个被测项目建立一个测试项目,并且为每个被测方法至少建立一个测试方法。 /// 使用以下规则对测试名称进行清晰的定义:[被测方法]_[场景]_[预期行为] /// 在测试中使用[TestInitialize]和[TestCleanup]特性来重用代码,例如新建和初始化所有测试都要用到的对象。 /// 不要在[TestInitialize]和[TestCleanup]中初始化或销毁并非所有测试都在使用的对象,否则会使测试难以理解。 /// </summary> [TestClass] public class UnitTest2 { Calculator calc = null; int count = 0; [TestInitialize] public void Initialize() { Console.WriteLine(count);// 0 calc = new Calculator(); count++; Console.WriteLine("Initialize"); } [TestCleanup] public void Cleanup() { Console.WriteLine(count); Console.WriteLine("Cleanup"); } [TestMethod] public void Sum_NoAddNums_DefaultsToZero() { int lastSum = calc.Sum(); Assert.AreEqual(0, lastSum); Console.WriteLine(count); } [TestMethod] public void Add() { calc.Add(4); Console.WriteLine(count); } [TestMethod] public void AddToSum() { calc.Add(1); int lastSum = calc.Sum(); Assert.AreEqual(1, lastSum); Console.WriteLine(count); } } public class LogAnalyzer { public bool IsValid(string fileName) { if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentException("No filename provider"); } return true; } } public class Calculator { private int sum = 0; public void Add(int number) { sum += number; } public int Sum() { return sum; } } }
  • 相关阅读:
    阿牛的EOF牛肉串
    盐水的故事
    密码
    Digital Roots
    不容易系列之(3)—— LELE的RPG难题
    不容易系列之一
    超级楼梯
    母牛的故事
    蟠桃记
    Children’s Queue
  • 原文地址:https://www.cnblogs.com/youknowUL/p/11385196.html
Copyright © 2011-2022 走看看