使用Github Copilot生成单元测试并执行
上一篇文章我们介绍了
本文我们继续使用Github Copilot在已有代码的基础上生成代码测试并执行。
一、先说一下代码的背景
需要上需要提供一个度量衡的工具类,实现各种转换,例如
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 Copilot生成单元测试并执行的更多相关文章
- Github Copilot 比在座各位更会写代码。jpg
之前大佬和我安利过 Copilot, 作为一个能用就行的践行者, 我一贯对这些东西都不太感兴趣. 就如我多年VS Code写各种编程语言, jetbrains 全家桶我都懒得搞~ 不过最近看到过Cha ...
- ANTLR和StringTemplate实例:自动生成单元测试类
ANTLR和StringTemplate实例:自动生成单元测试类 1. ANTLR语法 要想自动生成单元测试,首先第一步就是分析被测试类.这里以Java代码为例,用ANTLR对Java代码进行分析.要 ...
- Github Copilot 结合python的使用
之前提交的github copilot技术预览版申请,今天收到准入邮件,于是安上试一试这个准备把我送去电子厂上班的copy a lot ? 官网及申请地址:https://copilot.github ...
- 让 AI 为你写代码 - 体验 Github Copilot
前几天在群里看到有大神分享 Copoilot AI 写代码,看了几个截图有点不敢相信自己的眼睛.今天赶紧自己也来体验一下 Copoilot AI 写代码到底有多神奇. 申请 现在 Copoilot 还 ...
- NetBeans IDE 7.4 Beta版本build JavaFX时生成的可执行jar包执行时找不到依赖的jar包
现象,执行时抛出java.lang.ClassNotFoundException异常: Executing E:\secondegg\secondegg-reversi\dist\run8022211 ...
- PowerDesigner16.5 生成SQL脚本执行出错:collate chinese_prc_ci_as
PowerDesigner16.5 生成SQL脚本执行出错, collate chinese_prc_ci_as 点DataBase-edit current dbms —— 左边Script - O ...
- java之生成可重复执行的sql脚本
在实际项目开发过程中,sql脚本需要多次执行.而一般的DML和DDL语句一般只能执行一次,再次执行执行时就会报错(操作对应已存在/不存在),所以必须将sql脚本生成可重复执行的.本文共分为4部分:1. ...
- 如何通过源码生成Gatling可执行工具
其实,这个对于不是很熟系sbt的人来说,或者对scala语言没有什么了解的人,接触Gatling这个开源的性能测试框架,还是有些茫然的. 因为GitHub上提供的Gatling (最新版本:2.2.0 ...
- nodejs 中使用 mocha + should + jscoverage 生成 单元测试覆盖率报告
最近一直在做nodejs,而关于js的单元测试覆盖率网上资料比较少而且吧比较零散,我从网上找来一些资料整理一下分析给大家,希望大家可以少走弯路. 首先我是从windows环境下测试的,用到的工具有 m ...
- 如何在GitHub上生成ssh公钥并用NetBeans克隆项目
一.生成ssh公钥. 1.首先判断本机是否创建了公有密钥: $ ls ~/.ssh 这个命令用于检查是否已经存在 id_rsa.pub 或 id_dsa.pub 文件,如果文件已经存在,下面步骤可省略 ...
随机推荐
- [CF 1780B] GCD Partition
B. GCD Partition 题意 : 给一个长度为n的序列, 并将其分成连续的k块(k > 1), 得到序列b, 使得 \(gcd(b_{1}, b_{2}, b_{3}, ..., b_ ...
- 使用clip-path将 GIF 绘制成跳动的字母
前言 之前看到过一个有趣的CSS效果,今天我们也来实现一遍,将动图GIF通过clip-path绘制成一个个跳动的字母. 效果如下: GIF随便找的,嗯?这不是重点,重点是下面的实现过程,别被GIF吸引 ...
- 使用 virt-install 命令创建虚拟机
实践 参考文档:官方手册 这个命令适用于创建第一个虚拟机,后面如果再增加,修改xml文件或者使用clone命令就可以了. centos.sh #!/bin/bash name='centos7' is ...
- vue + canvas 实现涂鸦面板
前言 专栏分享:vue2源码专栏,vue router源码专栏,玩具项目专栏,硬核 推荐 欢迎各位 ITer 关注点赞收藏 此篇文章用于记录柏成从零开发一个canvas涂鸦面板的历程,最终效果如下: ...
- mybatis-plus+nacos配置中心和服务发现保姆级教程
默认你已经看了我的Mybatis-Plus+Mysql的教程,现在有了一个简单的项目如下(之前的教程: https://www.cnblogs.com/leafstar/p/17638741.htm ...
- 3.你所不知道的go语言控制语句——Leetcode习题69
目录 本篇前瞻 Leetcode习题9 题目描述 代码编写 控制结构 顺序结构(Sequence) 声明和赋值 多返回值赋值 运算符 算术运算符 位运算符 逻辑运算 分支结构 if 语句 switch ...
- Jmeter关联之正则表达式提取器
正则表达式简介 摘自网上的说法,正则表达式是对字符串(包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为"元字符"))操作的一种 逻辑公式,就是用事先定义好的一些特定字符 ...
- 《Kali渗透基础》07. 弱点扫描(一)
@ 目录 1:漏洞发现 1.1:Exploit-DB 1.2:searchsploit 1.3:nmap 2:漏洞管理 3:弱点扫描类型 4:漏洞基本概念 4.1:CVSS 4.2:CVE 4.3:O ...
- 如何调用API接口获取淘宝商品数据
淘宝商品数据的获取是一项非常重要的技术,它可以为淘宝卖家和买家提供有利的数据分析和扩展市场的机会.调用API接口是一种快速.方便.高效的方式获取淘宝商品数据. 以下是一些步骤来调用API接口来获取淘宝 ...
- CVE-2018-8120 漏洞复现
CVE-2018-8120 漏洞复现 漏洞描述 win32k.sys中函数 SetImeInfoEx未对指针进行合法性检查,从而导致一个任意地址写. 漏洞分析 漏洞成因 int __stdcall S ...