首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

超輕量級測試平台:OBP 控制台測試框架實作

     編輯:關於.NET

大型專案編譯耗時、自動化測試不足以覆蓋所有場景、有時需人工驗證。OBP 控制台測試框架 是極簡輕量類庫,無 GUI (非 WinForms/Web/Silverlight/WPF),專注於整合前驗證核心業務功能。

一、核心特性

  • 執行期透過自訂屬性 + 反射發現測試類別與方法
  • 提供簡單選單式控制台 UI
  • 支援巢狀選單重用 (父測試類調用子測試類 Execute)
  • 便利顯示業務物件集合、選擇單一物件測試

二、關鍵定義

  • 測試方法:驗證專案行為/動作/流程的方法
  • 測試類別:包含一或多個測試方法,可含共用測試成員
  • 選單:控制台顯示的活動清單,鍵盤觸發測試方法

三、架構設計

核心抽象類別 CustomConsoleTest:

  1. 反射掃描子類別方法,找出標記 [TestMethod] 的方法
  2. 讀取屬性顯示名稱,建立選單項目 (MethodItem 包含 MethodInfo 與顯示文字)
  3. 實例化子類別並呼叫 Execute() 進入測試迴圈
  4. 支援巢狀:測試方法可呼叫子測試類別的 Execute(),構建分層選單

四、核心實作程式碼

建構子:發現測試方法


public CustomConsoleTest() {
    var t = this.GetType();
    int i = 0; string name = null;
    foreach (var m in t.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
        if (IsValidMethod(m, ref name)) {
            var item = new MethodItem(name, m);
            _methods.Add(_menuKeys[i], item);
            ++i;
        }
    }
}

Execute:測試迴圈與選單顯示


public void Execute() {
    char c = '*';
    while ((c != 'q') && (c != 'Q')) {
        foreach (var p in _methods)
            Console.WriteLine("{0} - {1}", p.Key, p.Value.DisplayText);
        Console.WriteLine("q - Quit");
        Console.WriteLine();
        c = Console.ReadKey(true).KeyChar;
        if (_methods.ContainsKey(c))
            try {
                var m = _methods[c];
                m.Method.Invoke(this, null);
            } catch (Exception ex) {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("An exception has occured when invoking the method {0}", ex);
            }
    }
}

DisplayCollection:泛型集合顯示


protected void DisplayCollection<T>(IEnumerable<T> aCollection) {
    if (aCollection.Count() == 0) Console.WriteLine("No item in the collection");
    int i = 1; _elts.Clear();
    foreach (var e in aCollection) {
        Console.WriteLine("{0} - {1}", i, GetDescription(e));
        _elts.Add(i, e);
        ++i;
    }
    Console.WriteLine();
}

SelectItem:互動式選擇單一項目


protected T SelectItem<T>(IEnumerable<T> aCollection) where T : class {
    DisplayCollection<T>(aCollection);
    while (true) {
        Console.WriteLine("Type the element number or q to exit");
        var text = Console.ReadLine();
        if (text == "q" || text == "Q") return null;
        try {
            int index = Convert.ToInt32(text);
            if (_elts.ContainsKey(index)) return (T)_elts[index];
            else Console.WriteLine("The list does not contain that number, try again !");
        } catch { Console.WriteLine("Wrong value"); }
    }
}

MethodItem:選單項目封裝


class MethodItem {
    internal MethodItem(string aDisplayText, MethodInfo aMethod) {
        DisplayText = aDisplayText; Method = aMethod;
    }
    internal string DisplayText { get; private set; }
    internal MethodInfo Method { get; private set; }
}

五、範例:機器與零組件測試

業務物件:Machine (含 List<SparePart>)、SparePart。三測試類:

  • MachineTest:列出機器、自訂 GetDescription 顯示名稱/重量/零件數
  • PartTest:選擇單一零件測試,使用 SelectItem
  • SoftwareTest:根選單,巢狀調用 MachineTest.Execute()、PartTest.Execute()

class SoftwareTest : CustomConsoleTest {
    private MachineTest _machineTest = new MachineTest();
    private PartTest _partTest = new PartTest();

    [TestMethod("Machines")] private void ExecuteMachineTest() { _machineTest.Execute(); }
    [TestMethod("Parts")] private void ExecutePartTest() { _partTest.Execute(); }

    static SoftwareTest() { Machine.CreateInstances(); SparePart.CreateInstances(); }
}

六、總結

自動化測試雖好,特殊情境仍需人工介入。控制台測試無 GUI 開銷、啟動極快、專注核心業務驗證,是大型系統開發中極具實戰價值的補充驗證手段。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved