使用Github Copilot生成单元测试并执行

上一篇文章我们介绍了

使用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

 

posted @ 2023-11-06 09:21  Eric zhou  阅读(4173)  评论(2编辑  收藏  举报