看代码吧
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace P2P.Tests { [TestClass()] public sealed class DivideClassTest { [AssemblyInitialize()] public static void AssemblyInit(TestContext context) { //配置log4net //var path = AppDomain.CurrentDomain.BaseDirectory + ("\\log4net.config"); //System.IO.FileInfo file = new System.IO.FileInfo(path); //log4net.Config.XmlConfigurator.ConfigureAndWatch(file); MessageBox.Show("AssemblyInit " + context.TestName); } [ClassInitialize()] public static void ClassInit(TestContext context) { MessageBox.Show("ClassInit " + context.TestName); } [TestInitialize()] public void Initialize() { MessageBox.Show("TestMethodInit"); } [TestCleanup()] public void Cleanup() { MessageBox.Show("TestMethodCleanup"); } [ClassCleanup()] public static void ClassCleanup() { MessageBox.Show("ClassCleanup"); } [AssemblyCleanup()] public static void AssemblyCleanup() { MessageBox.Show("AssemblyCleanup"); } [TestMethod()] [ExpectedException(typeof(System.DivideByZeroException))] public void DivideMethodTest() { DivideClass.DivideMethod(0); } } public class DivideClass { public static int DivideMethod(int denominator) { return (2 / denominator); } } }