首页 > 其他分享 >使用Github Copilot生成单元测试并执行

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

时间:2023-11-06 09:34:32浏览次数:29  
标签:Github UnitTesting 单元测试 value measurementService Copilot VisualStudio LengthUnit

上一篇文章我们介绍了

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

 

标签:Github,UnitTesting,单元测试,value,measurementService,Copilot,VisualStudio,LengthUnit
From: https://www.cnblogs.com/tianqing/p/17810231.html

相关文章

  • 【Git 教程系列第 27 篇】ssh: connect to host github.com port 22: Connection refu
    https://blog.csdn.net/qq_42351033/article/details/131612279ssh:connecttohostgithub.comport22:Connectiontimedoutfatal:Couldnotreadfromremoterepo......
  • github与git使用
    参考:<<从0开始学习github系列>>(微信公众号stormzhang中)1.首次传输本地文件到github1.1前期准备github创建账号与仓库主机添加ssh(非必须,用于免密登录,一台主机设置完即可不用重复设置)主机添加ssh步骤1.命令行中输入ssh检查ssh是否存在,出现下图说明存在,不......
  • 单元测试编写
      @SpringBootTest@RunWith(SpringJUnit4ClassRunner.class)publicclassHelloTest{@AutowiredprivateSysDictionaryDaodictionaryDao;@Beforepublicvoidbefore(){TableInfoHelper.initTableInfo(newMapperBuilderAssista......
  • c#中使用METest单元测试
    METest是一个用于测试C#代码的单元测试框架。单元测试是一种软件测试方法,用于验证代码的各个单元(函数、方法、类等)是否按照预期工作。METest提供了一种简单而强大的方式来编写和运行单元测试。TestMethod:这是一个特性,用于标记测试方法。Assert:这是一个断言类,用于验证测试结果是......
  • c#中使用METest单元测试
    METest是一个用于测试C#代码的单元测试框架。单元测试是一种软件测试方法,用于验证代码的各个单元(函数、方法、类等)是否按照预期工作。METest提供了一种简单而强大的方式来编写和运行单元测试。TestMethod:这是一个特性,用于标记测试方法。Assert:这是一个断言类,用于验证测试结果是......
  • [MDP.NetCore] 開發一個從GitHub持續佈署到Azure Container Apps的Web站台
    開發一個從GitHub持續佈署到AzureContainerApps的Web站台程式碼簽入GitHub之後,啟動GitHubAction流程,編譯並部署程式到AzureContainerApps,是開發系統時常見的功能需求。本篇範例協助開發人員使用GitHub與Azure,逐步完成必要的設計和實作。操作步驟1.註冊並登入AzurePortal......
  • 将GitHub上的forked仓库删除
    内容来自DOChttps://q.houxu6.top/?s=将GitHub上的forked仓库删除我开始使用git和GitHub,有一个项目我正在GitHub上关注。我不小心点击了fork它。现在它似乎成了一个新的项目。我对这件事有一些疑问:我知道如果我对我的forked仓库进行提交或其他操作,它将会被更新,但是更新后......
  • 2021年github文件高速下载方法
     https://shrill-pond-3e81.hunsh.workers.dev/  ......
  • 问题记录 <VSCode Copilot 连接问题:Extension activation failed: "getaddrinfo EAI_A
    问题描述VSCode使用Copilot时遇到如下问题:Extensionactivationfailed:"getaddrinfoEAI_AGAINapi.github.com"解决方式笔者尝试了修改hosts、代理、重装插件等方法,但没有起效。下面的方法解决了问题(在VSCode中设置proxy)打开代理,查看代理http地址,复制;打开VSCode,打......
  • 发现一种增加在 GitHub 曝光量的方法,已举报
    今天偶然看到一种增加项目和个人在GitHub曝光量的方法,但感觉无法赞同这种做法,已经向GitHub官方举报。具体怎么回事呢?我上周在Vim插件大佬tpope的一个项目提了个Issue,但一周过去了,大佬也没有回应,我就去他的GitHub主页确认他这一周有没有活动记录,看到他最近的提交活动是给......