日期:2014-05-18 浏览次数:20820 次
端午节有空本来是为整理一下C#调用C/C++库的方法,为了测试用例顺便实现了一直想实现的一个小的测试框架跟同事们分享一下,NUnit搞得已经比较复杂了,这个非常简单理解起来容易,用起来比较方便,谁再想用其它功能再自己加吧,如果功能要求比较多就直接用NUnit好了,不要再自己造轮子了。
此篇献给伟大的屈原,是他给我们创造了这样一个假期!
代码比较简单,注释也写了不少,就不再多说了,直接上代码,时间仓促,不保证没有BUG:
/** * A mini test framework: TTest * Noock.tian@gmail.com * version: 0.1 * updated: 2012-06-24 * * Usage: * 1) Write a static test case method to do test, which must need no arguments; * 2) Tag the test method with TestCaseAttribute attribute; * 3) Call Test.Run( ) functions at anytime; * * If a log file is needed, we have at least two methods to get that: * 1) Run the test in a console and redirect the output to a file, like this: * .\myapp.exe > test.log * 2) Set the TTest.Out, like this * using (FileStream fs = new FileStream(string.Format("Test_{0}.log", DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")),FileMode.CreateNew)) { using (StreamWriter sw = new StreamWriter(fs)) { Test.Out = sw; Test.Run(); Test.Out = Console.Out; } } * 3) You must know, create it! */ using System; using System.Diagnostics; using System.IO; using System.Reflection; namespace Noock.TTest { /// <summary> /// An attribute which indicate that the method is a test case, it will be run by the test engine. /// </summary> /// <remarks> /// A test case method should: /// 1) requires no arguments; /// 2) static; /// 3) private (recommend) /// 4) returns void (recommend); /// </remarks> public class TestCaseAttribute : System.Attribute { public string Target { get; set; } } /// <summary> /// Indicates a failed test case /// </summary> public class TestFailedException : Exception { public TestFailedException(string msg) : base(msg) { } } /// <summary> /// Test engine /// </summary> /// <remarks> /// <para>Call Equals functions for condition check for every test case method</para> /// <para>call a Run( ) function to start test</para> /// </remarks> public static class TTest { #region Condition checkers /// <summary> /// Compare two int values /// </summary> /// <param name="expect">expected value</param> /// <param name="actual">actual value</param> /// <param name="msg">a message to display if failed</param> public static void AreEqual(int expect, int actual, string msg = null) { if (expect != actual) throw new TestFailedException(msg ?? string.Format("Values not equals: expect {0}, got {1}", expect, actual)); } /// <summary> /// Compare two double values /// </summary> /// <param name="expect">expected value</param> /// <param name="actual">actual value</param> /// <param name="msg">a message to display if failed</param> public static void AreEqual(double expect, double actual, string msg = null) { if (Math.Abs(expect - actual) > double.Epsilon) throw new TestFailedException(msg ?? string.Format("Values not equals: expect {0}, got {1}", expect, actual)); } /// <summary> /// Compare two strings /// </summary> /// <param name="expect">expected value</param> /// <param name="actual">actual value</param> /// <param name="msg">a message to display if failed</param> /// <param name="ignoreCase">true: case insensitive, false: case sensitive</param> public static void AreEqual(string expect, string actual, bool ignoreCase = false, string msg = null) { if( !expect.Equals(actual, ignoreCase ? StringComparison.InvariantCultureIgnoreCase : StringComparison.InvariantCulture) ) throw new TestFailedException(msg ?? string.Format("