使用Github Copilot生成單元測試並執行

2023-11-06 12:02:22

上一篇文章我們介紹了

使用Github Copilot完成程式碼編寫

本文我們繼續使用Github Copilot在已有程式碼的基礎上生成程式碼測試並執行。

一、先說一下程式碼的背景

需要上需要提供一個度量衡的工具類,實現各種轉換,例如

將長度值轉換為英寸
將長度值轉換為英里
將長度值轉換為釐米
將長度值轉換為千米
將重量值轉換為克
將重量值轉換為千克
將功率值轉換為瓦特
將電流值轉換為安培
將電壓值轉換為伏特
......
需要比較簡單明確,可以參照一個三方庫(https://digidemic.github.io/UnitOf/),但是太重了,所以用Github Copilot直接生成程式碼實現了。
二、使用Github Copilot生成程式碼
首先需要組織好Prompt,這個至關重要,好的Prompt會事半功倍,不過需要多次嘗試,才能把Prompt設計好
這裡Share一下Prompt的設計思路:
1. 先提供幾個列舉類
Prompt:請用C#生成一個列舉類LengthUnit,包含各種常見的長度單位

 Prompt:請用C#生成一個列舉類WeightUnit,包含各種常見的重量單位,幷包含中文註釋,標準的註釋格式

 這個Prompt跟進一步了

Prompt: 請用C#生成一個列舉類PowerUnit,包含各種常見的功率單位,幷包含中文註釋,標準的註釋格式

Prompt: 請用C#生成一個列舉類CurrentUnit,包含各種常見的電流單位,幷包含中文註釋,標準的註釋格式

Prompt 請用C#生成一個列舉類CurrentUnit,包含各種常見的電壓單位,幷包含中文註釋,標準的註釋格式

 2. 用C#生成一個提供度量衡服務的實現類MeasurementService

這個Prompt設計了很久:

Prompt:請用C#生成一個提供度量衡服務的實現類MeasurementService,它提供了以下方法將長度值轉換為英寸、長度值轉換為英尺、 將長度值轉換為英里、 將長度值轉換為釐米、 將長度值轉換為千米、 將重量值轉換為克、 將重量值轉換為千克、 將功率值轉換為瓦特、 將電流值轉換為安培、 將電壓值轉換為伏特。例如將長度值轉換為英寸的實現方法是public double ConvertToInch(double value, LengthUnit lengthUnit),這個方法中將變數LengthUnit,做長度轉換。方法請新增標準註釋,使用中文註釋。

 生成的程式碼

public class MeasurementService
{
    /// <summary>
    /// 將長度值轉換為英寸
    /// </summary>
    public double ConvertToInch(double value, LengthUnit lengthUnit)
    {
        switch (lengthUnit)
        {
            case LengthUnit.Meter:
                return value * 39.3701;
            case LengthUnit.Centimeter:
                return value * 0.393701;
            case LengthUnit.Kilometer:
                return value * 39370.1;
            case LengthUnit.Mile:
                return value * 63360;
            case LengthUnit.Foot:
                return value * 12;
            case LengthUnit.Yard:
                return value * 36;
            default:
                throw new NotSupportedException("The provided LengthUnit is not supported.");
        }
    }

    /// <summary>
    /// 將長度值轉換為英尺
    /// </summary>
    public double ConvertToFoot(double value, LengthUnit unit)
    {
        // Conversion logic here
    }

    /// <summary>
    /// 將長度值轉換為英里
    /// </summary>
    public double ConvertToMile(double value, LengthUnit unit)
    {
        // Conversion logic here
    }

    /// <summary>
    /// 將長度值轉換為釐米
    /// </summary>
    public double ConvertToCentimeter(double value, LengthUnit unit)
    {
        // Conversion logic here
    }

    /// <summary>
    /// 將長度值轉換為千米
    /// </summary>
    public double ConvertToKilometer(double value, LengthUnit unit)
    {
        // Conversion logic here
    }

    /// <summary>
    /// 將重量值轉換為克
    /// </summary>
    public double ConvertToGram(double value, WeightUnit unit)
    {
        // Conversion logic here
    }

    /// <summary>
    /// 將重量值轉換為千克
    /// </summary>
    public double ConvertToKilogram(double value, WeightUnit unit)
    {
        // Conversion logic here
    }

    /// <summary>
    /// 將功率值轉換為瓦特
    /// </summary>
    public double ConvertToWatt(double value, PowerUnit unit)
    {
        // Conversion logic here
    }

    /// <summary>
    /// 將電流值轉換為安培
    /// </summary>
    public double ConvertToAmpere(double value, CurrentUnit unit)
    {
        // Conversion logic here
    }

    /// <summary>
    /// 將電壓值轉換為伏特
    /// </summary>
    public double ConvertToVolt(double value, VoltageUnit unit)
    {
        // Conversion logic here
    }
}

這裡你會發現,其他方法未實現,需要繼續告訴Github Copilot繼續生成程式碼

Prompt:MeasurementService類中,請參考ConvertToInch這個方法的實現方式和註釋要求,繼續生成ConvertToMile,ConvertToFoot,ConvertToCentimeter,ConvertToKilometer,ConvertToGram,ConvertToKilogram,ConvertToWatt,ConvertToAmpere,ConvertToVolt等方法的具體轉換邏輯,每個方法都要實現程式碼輸出

Github Copilot生成的程式碼不一定全面準確,需要做程式碼確認,這個很關鍵,以下是人工確認後的程式碼(改了不少地方)

/// <summary>
    /// 提供度量衡服務的實現類
    /// </summary>
    public class MeasurementService : IMeasurementService
    {
        /// <summary>
        /// 將長度值轉換為英寸
        /// </summary>
        public double ConvertToInch(double value, LengthUnit lengthUnit)
        {
            switch (lengthUnit)
            {
                case LengthUnit.Inch:
                    return value;
                case LengthUnit.Meter:
                    return value * 39.3701;
                case LengthUnit.Centimeter:
                    return value * 0.393701;
                case LengthUnit.Kilometer:
                    return value * 39370.1;
                case LengthUnit.Mile:
                    return value * 63360;
                case LengthUnit.Foot:
                    return value * 12;
                case LengthUnit.Yard:
                    return value * 36;
                default:
                    throw new NotSupportedException("The provided LengthUnit is not supported.");
            }
        }

        /// <summary>
        /// 將長度值轉換為英里
        /// </summary>
        public double ConvertToMile(double value, LengthUnit lengthUnit)
        {

            // 根據長度單位進行轉換
            switch (lengthUnit)
            {
                case LengthUnit.Mile:
                    return value;
                case LengthUnit.Centimeter:
                    return value / 160934.4;
                case LengthUnit.Inch:
                    return value / 63360;
                case LengthUnit.Kilometer:
                    return value / 1.609344;
                case LengthUnit.Yard:
                    return value / 1760;
                case LengthUnit.Meter:
                    return value / 1609.344;
                case LengthUnit.Foot:
                    return value / 5280;
                default:
                    throw new NotSupportedException($"Unsupported length unit: {lengthUnit}");
            }
        }

        /// <summary>
        /// 將長度值轉換為英尺
        /// </summary>
        public double ConvertToFoot(double value, LengthUnit lengthUnit)
        {
            // 根據長度單位進行轉換
            switch (lengthUnit)
            {
                case LengthUnit.Foot:
                    return value;
                case LengthUnit.Meter:
                    return value / 0.3048;
                case LengthUnit.Centimeter:
                    return value / 30.48;
                case LengthUnit.Mile:
                    return value * 5280;
                case LengthUnit.Kilometer:
                    return value * 3280.84;
                case LengthUnit.Yard:
                    return value * 3;
                case LengthUnit.Inch:
                    return value * 12;
                default:
                    throw new NotSupportedException($"Unsupported length unit: {lengthUnit}");
            }
        }

        /// <summary>
        /// 將長度值轉換為釐米
        /// </summary>
        public double ConvertToCentimeter(double value, LengthUnit lengthUnit)
        {
            // 根據長度單位進行轉換
            switch (lengthUnit)
            {
                case LengthUnit.Centimeter:
                    return value;
                case LengthUnit.Meter:
                    return value * 100;
                case LengthUnit.Inch:
                    return value * 2.54;
                case LengthUnit.Mile:
                    return value * 160934.4;
                case LengthUnit.Kilometer:
                    return value * 100000;
                case LengthUnit.Yard:
                    return value * 91.44;
                case LengthUnit.Foot:
                    return value * 30.48;
                default:
                    throw new NotSupportedException($"Unsupported length unit: {lengthUnit}");
            }
        }

        /// <summary>
        /// 將長度值轉換為千米
        /// </summary>
        public double ConvertToKilometer(double value, LengthUnit lengthUnit)
        {
            // 根據長度單位進行轉換
            switch (lengthUnit)
            {
                case LengthUnit.Meter:
                    return value / 1000;
                case LengthUnit.Inch:
                    return value / 39370.1;
                case LengthUnit.Mile:
                    return value / 1.609344;
                case LengthUnit.Yard:
                    return value / 1093.6133;
                case LengthUnit.Foot:
                    return value / 3280.8399;
                case LengthUnit.Kilometer:
                    return value;
                case LengthUnit.Centimeter:
                    return value / 100000;
                default:
                    throw new NotSupportedException($"Unsupported length unit: {lengthUnit}");
            }
        }

        /// <summary>
        /// 將重量值轉換為克
        /// </summary>
        public double ConvertToGram(double value, WeightUnit weightUnit)
        {
            // 根據重量單位進行轉換
            switch (weightUnit)
            {
                case WeightUnit.Gram:
                    return value;
                case WeightUnit.Kilogram:
                    return value * 1000;
                case WeightUnit.Pound:
                    return value * 453.59237;
                case WeightUnit.Tonne:
                    return value * 1000000;
                case WeightUnit.Ounce:
                    return value * 28.349523125;
                case WeightUnit.Milligram:
                    return value / 1000;
                default:
                    throw new NotSupportedException($"Unsupported weight unit: {weightUnit}");
            }
        }

        /// <summary>
        /// 將重量值轉換為千克
        /// </summary>
        public double ConvertToKilogram(double value, WeightUnit weightUnit)
        {
            // 根據重量單位進行轉換
            switch (weightUnit)
            {
                case WeightUnit.Kilogram:
                    return value;
                case WeightUnit.Gram:
                    return value / 1000;
                case WeightUnit.Pound:
                    return value * 0.45359237;
                case WeightUnit.Tonne:
                    return value * 1000;
                case WeightUnit.Ounce:
                    return value * 0.028349523125;
                case WeightUnit.Milligram:
                    return value / 1000000;
                default:
                    throw new NotSupportedException($"Unsupported weight unit: {weightUnit}");
            }
        }

        /// <summary>
        /// 將功率值轉換為瓦特
        /// </summary>
        public double ConvertToWatt(double value, PowerUnit powerUnit)
        {
            // 根據功率單位進行轉換
            switch (powerUnit)
            {
                case PowerUnit.Watt:
                    return value;
                case PowerUnit.Kilowatt:
                    return value * 1000;
                case PowerUnit.Horsepower:
                    return value * 745.699872;
                case PowerUnit.Megawatt:
                    return value * 1000000;
                default:
                    throw new NotSupportedException($"Unsupported power unit: {powerUnit}");
            }
        }

        /// <summary>
        /// 將電流值轉換為安培
        /// </summary>
        public double ConvertToAmpere(double value, CurrentUnit currentUnit)
        {
            // 根據電流單位進行轉換
            switch (currentUnit)
            {
                case CurrentUnit.Ampere:
                    return value;
                case CurrentUnit.Milliampere:
                    return value / 1000;
                case CurrentUnit.Microampere:
                    return value / 1000000;
                case CurrentUnit.Kiloampere:
                    return value * 1000;
                default:
                    throw new NotSupportedException($"Unsupported current unit: {currentUnit}");
            }
        }

        /// <summary>
        /// 將電壓值轉換為伏特
        /// </summary>
        public double ConvertToVolt(double value, VoltageUnit voltageUnit)
        {
            // 根據電壓單位進行轉換
            switch (voltageUnit)
            {
                case VoltageUnit.Volt:
                    return value;
                case VoltageUnit.Millivolt:
                    return value / 1000;
                case VoltageUnit.Microvolt:
                    return value / 1000000;
                case VoltageUnit.Kilovolt:
                    return value * 1000;
                default:
                    throw new NotSupportedException($"Unsupported voltage unit: {voltageUnit}");
            }
        }
    }

3. 生成單元測試程式碼

首先選擇整個類,然後輸入以下Prompt

@workspace /tests  請對選中的程式碼,使用MSTest單元測試框架,生成單元測試程式碼,請為每個方法都實現單元測試

 

 每次僅生成了一個方法的單元測試,可以繼續使用以下Prompt

Repeat similar tests for other methods:ConvertToMile, ConvertToFoot, ConvertToCentimeter, ConvertToKilometer, ConvertToGram, ConvertToKilogram, ConvertToWatt, ConvertToAmpere, ConvertToVolt

最終生成的UnitTest程式碼

using Microsoft.VisualStudio.TestTools.UnitTesting;
using T.Core.i10n.Service;
using T.Core.i10n.SPI;

namespace T.Core.i10n.Service.Tests
{
    [TestClass]
    public class MeasurementServiceTests
    {
        private MeasurementService? _measurementService;

        [TestInitialize]
        public void Setup()
        {
            _measurementService = new MeasurementService();
        }

        [TestMethod]
        public void ConvertToInch_ShouldConvertCorrectly()
        {
            var result = _measurementService!.ConvertToInch(1, LengthUnit.Meter);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(39.3701, result, 0.0001);
        }

        [TestMethod]
        [ExpectedException(typeof(NotSupportedException))]
        public void ConvertToInch_ShouldThrowExceptionForUnsupportedUnit()
        {
            _measurementService!.ConvertToInch(1, (LengthUnit)999);
        }

        [TestMethod]
        public void ConvertToMile_ShouldConvertCorrectly()
        {
            var result = _measurementService!.ConvertToMile(1, LengthUnit.Kilometer);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.621371, result, 0.0001);
        }

        [TestMethod]
        [ExpectedException(typeof(NotSupportedException))]
        public void ConvertToMile_ShouldThrowExceptionForUnsupportedUnit()
        {
            _measurementService!.ConvertToMile(1, (LengthUnit)999);
        }

        [TestMethod]
        public void ConvertToFoot_ShouldConvertCorrectly()
        {
            var result = _measurementService!.ConvertToFoot(1, LengthUnit.Meter);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(3.28084, result, 0.0001);
        }

        [TestMethod]
        [ExpectedException(typeof(NotSupportedException))]
        public void ConvertToFoot_ShouldThrowExceptionForUnsupportedUnit()
        {
            _measurementService!.ConvertToFoot(1, (LengthUnit)999);
        }

        [TestMethod]
        public void ConvertToCentimeter_ShouldConvertCorrectly()
        {
            var result = _measurementService!.ConvertToCentimeter(1, LengthUnit.Meter);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(100, result, 0.0001);
        }

        [TestMethod]
        [ExpectedException(typeof(NotSupportedException))]
        public void ConvertToCentimeter_ShouldThrowExceptionForUnsupportedUnit()
        {
            _measurementService!.ConvertToCentimeter(1, (LengthUnit)999);
        }

        [TestMethod]
        public void ConvertToKilometer_ShouldConvertCorrectly()
        {
            var result = _measurementService!.ConvertToKilometer(1, LengthUnit.Meter);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, result, 0.0001);
        }

        [TestMethod]
        [ExpectedException(typeof(NotSupportedException))]
        public void ConvertToKilometer_ShouldThrowExceptionForUnsupportedUnit()
        {
            _measurementService!.ConvertToKilometer(1, (LengthUnit)999);
        }

        [TestMethod]
        public void ConvertToGram_ShouldConvertCorrectly()
        {
            var result = _measurementService!.ConvertToGram(1, WeightUnit.Kilogram);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, result, 0.0001);
        }

        [TestMethod]
        [ExpectedException(typeof(NotSupportedException))]
        public void ConvertToGram_ShouldThrowExceptionForUnsupportedUnit()
        {
            _measurementService!.ConvertToGram(1, (WeightUnit)999);
        }

        [TestMethod]
        public void ConvertToKilogram_ShouldConvertCorrectly()
        {
            var result = _measurementService!.ConvertToKilogram(1, WeightUnit.Gram);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, result, 0.0001);
        }

        [TestMethod]
        [ExpectedException(typeof(NotSupportedException))]
        public void ConvertToKilogram_ShouldThrowExceptionForUnsupportedUnit()
        {
            _measurementService!.ConvertToKilogram(1, (WeightUnit)999);
        }

        [TestMethod]
        public void ConvertToWatt_ShouldConvertCorrectly()
        {
            var result = _measurementService!.ConvertToWatt(1, PowerUnit.Kilowatt);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, result, 0.0001);
        }

        [TestMethod]
        [ExpectedException(typeof(NotSupportedException))]
        public void ConvertToWatt_ShouldThrowExceptionForUnsupportedUnit()
        {
            _measurementService!.ConvertToWatt(1, (PowerUnit)999);
        }

        [TestMethod]
        public void ConvertToAmpere_ShouldConvertCorrectly()
        {
            var result = _measurementService!.ConvertToAmpere(1, CurrentUnit.Milliampere);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, result, 0.0001);
        }

        [TestMethod]
        [ExpectedException(typeof(NotSupportedException))]
        public void ConvertToAmpere_ShouldThrowExceptionForUnsupportedUnit()
        {
            _measurementService!.ConvertToAmpere(1, (CurrentUnit)999);
        }

        [TestMethod]
        public void ConvertToVolt_ShouldConvertCorrectly()
        {
            var result = _measurementService!.ConvertToVolt(1, VoltageUnit.Millivolt);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, result, 0.0001);
        }

        [TestMethod]
        [ExpectedException(typeof(NotSupportedException))]
        public void ConvertToVolt_ShouldThrowExceptionForUnsupportedUnit()
        {
            _measurementService!.ConvertToVolt(1, (VoltageUnit)999);
        }
        [TestMethod]
        public void ConvertToInch_ShouldConvertCorrectly_ForEachLengthUnit()
        {
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToInch(1, LengthUnit.Inch), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(39.3701, _measurementService!.ConvertToInch(1, LengthUnit.Meter), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.393701, _measurementService!.ConvertToInch(1, LengthUnit.Centimeter), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(39370.1, _measurementService!.ConvertToInch(1, LengthUnit.Kilometer), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(63360, _measurementService!.ConvertToInch(1, LengthUnit.Mile), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(12, _measurementService!.ConvertToInch(1, LengthUnit.Foot), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(36, _measurementService!.ConvertToInch(1, LengthUnit.Yard), 0.0001);
        }

        [TestMethod]
        public void ConvertToMile_ShouldConvertCorrectly_ForEachLengthUnit()
        {
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToMile(1, LengthUnit.Mile), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0000062137, _measurementService!.ConvertToMile(1, LengthUnit.Centimeter), 0.0000000001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0000157828, _measurementService!.ConvertToMile(1, LengthUnit.Inch), 0.0000000001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.621371, _measurementService!.ConvertToMile(1, LengthUnit.Kilometer), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0005681818, _measurementService!.ConvertToMile(1, LengthUnit.Yard), 0.0000000001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0006213712, _measurementService!.ConvertToMile(1, LengthUnit.Meter), 0.0000000001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0001893939, _measurementService!.ConvertToMile(1, LengthUnit.Foot), 0.0000000001);
        }

        [TestMethod]
        public void ConvertToFoot_ShouldConvertCorrectly_ForEachLengthUnit()
        {
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToFoot(1, LengthUnit.Foot), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(3.28084, _measurementService!.ConvertToFoot(1, LengthUnit.Meter), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0328084, _measurementService!.ConvertToFoot(1, LengthUnit.Centimeter), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(3280.84, _measurementService!.ConvertToFoot(1, LengthUnit.Kilometer), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(5280, _measurementService!.ConvertToFoot(1, LengthUnit.Mile), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(3, _measurementService!.ConvertToFoot(1, LengthUnit.Yard), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0833333, _measurementService!.ConvertToFoot(1, LengthUnit.Inch), 0.0001);
        }

        [TestMethod]
        public void ConvertToCentimeter_ShouldConvertCorrectly_ForEachLengthUnit()
        {
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToCentimeter(1, LengthUnit.Centimeter), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(100, _measurementService!.ConvertToCentimeter(1, LengthUnit.Meter), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(2.54, _measurementService!.ConvertToCentimeter(1, LengthUnit.Inch), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(100000, _measurementService!.ConvertToCentimeter(1, LengthUnit.Kilometer), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(160934.4, _measurementService!.ConvertToCentimeter(1, LengthUnit.Mile), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(91.44, _measurementService!.ConvertToCentimeter(1, LengthUnit.Yard), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(30.48, _measurementService!.ConvertToCentimeter(1, LengthUnit.Foot), 0.0001);
        }

        // Continue with similar tests for the other methods...
        [TestMethod]
        public void ConvertToKilometer_ShouldConvertCorrectly_ForEachLengthUnit()
        {
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToKilometer(1, LengthUnit.Kilometer), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, _measurementService!.ConvertToKilometer(1, LengthUnit.Meter), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0000254, _measurementService!.ConvertToKilometer(1, LengthUnit.Inch), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1.609344, _measurementService!.ConvertToKilometer(1, LengthUnit.Mile), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0009144, _measurementService!.ConvertToKilometer(1, LengthUnit.Yard), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0003048, _measurementService!.ConvertToKilometer(1, LengthUnit.Foot), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.00001, _measurementService!.ConvertToKilometer(1, LengthUnit.Centimeter), 0.0001);
        }

        [TestMethod]
        public void ConvertToGram_ShouldConvertCorrectly_ForEachWeightUnit()
        {
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToGram(1, WeightUnit.Gram), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, _measurementService!.ConvertToGram(1, WeightUnit.Kilogram), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(453.59237, _measurementService!.ConvertToGram(1, WeightUnit.Pound), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000000, _measurementService!.ConvertToGram(1, WeightUnit.Tonne), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(28.349523125, _measurementService!.ConvertToGram(1, WeightUnit.Ounce), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, _measurementService!.ConvertToGram(1, WeightUnit.Milligram), 0.0001);
        }        

        [TestMethod]
        public void ConvertToKilogram_ShouldConvertCorrectly_ForEachWeightUnit()
        {
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToKilogram(1, WeightUnit.Kilogram), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, _measurementService!.ConvertToKilogram(1, WeightUnit.Gram), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.453592, _measurementService!.ConvertToKilogram(1, WeightUnit.Pound), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, _measurementService!.ConvertToKilogram(1, WeightUnit.Tonne), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0283495, _measurementService!.ConvertToKilogram(1, WeightUnit.Ounce), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.000001, _measurementService!.ConvertToKilogram(1, WeightUnit.Milligram), 0.0001);
        }

        [TestMethod]
        public void ConvertToWatt_ShouldConvertCorrectly_ForEachPowerUnit()
        {
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToWatt(1, PowerUnit.Watt), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, _measurementService!.ConvertToWatt(1, PowerUnit.Kilowatt), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(745.7, _measurementService!.ConvertToWatt(1, PowerUnit.Horsepower), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000000, _measurementService!.ConvertToWatt(1, PowerUnit.Megawatt), 0.0001);
        }

        [TestMethod]
        public void ConvertToAmpere_ShouldConvertCorrectly_ForEachCurrentUnit()
        {
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToAmpere(1, CurrentUnit.Ampere), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, _measurementService!.ConvertToAmpere(1, CurrentUnit.Milliampere), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.000001, _measurementService!.ConvertToAmpere(1, CurrentUnit.Microampere), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, _measurementService!.ConvertToAmpere(1, CurrentUnit.Kiloampere), 0.0001);
        }

        [TestMethod]
        public void ConvertToVolt_ShouldConvertCorrectly_ForEachVoltageUnit()
        {
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToVolt(1, VoltageUnit.Volt), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, _measurementService!.ConvertToVolt(1, VoltageUnit.Millivolt), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.000001, _measurementService!.ConvertToVolt(1, VoltageUnit.Microvolt), 0.0001);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, _measurementService!.ConvertToVolt(1, VoltageUnit.Kilovolt), 0.0001);
        }

        // Continue with similar tests for the other methods...
    }
}

以上單元測試程式碼,直接在VS Code 右鍵Run Test即可。

 

 

周國慶

2023/11/5