经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » Git » 查看文章
使用Github Copilot生成单元测试并执行
来源:cnblogs  作者:Eric zhou  时间:2023/11/6 9:23:42  对本文有异议

上一篇文章我们介绍了

使用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,做长度转换。方法请添加标准注释,使用中文注释。

 生成的代码

  1. public class MeasurementService
  2. {
  3. /// <summary>
  4. /// 将长度值转换为英寸
  5. /// </summary>
  6. public double ConvertToInch(double value, LengthUnit lengthUnit)
  7. {
  8. switch (lengthUnit)
  9. {
  10. case LengthUnit.Meter:
  11. return value * 39.3701;
  12. case LengthUnit.Centimeter:
  13. return value * 0.393701;
  14. case LengthUnit.Kilometer:
  15. return value * 39370.1;
  16. case LengthUnit.Mile:
  17. return value * 63360;
  18. case LengthUnit.Foot:
  19. return value * 12;
  20. case LengthUnit.Yard:
  21. return value * 36;
  22. default:
  23. throw new NotSupportedException("The provided LengthUnit is not supported.");
  24. }
  25. }
  26. /// <summary>
  27. /// 将长度值转换为英尺
  28. /// </summary>
  29. public double ConvertToFoot(double value, LengthUnit unit)
  30. {
  31. // Conversion logic here
  32. }
  33. /// <summary>
  34. /// 将长度值转换为英里
  35. /// </summary>
  36. public double ConvertToMile(double value, LengthUnit unit)
  37. {
  38. // Conversion logic here
  39. }
  40. /// <summary>
  41. /// 将长度值转换为厘米
  42. /// </summary>
  43. public double ConvertToCentimeter(double value, LengthUnit unit)
  44. {
  45. // Conversion logic here
  46. }
  47. /// <summary>
  48. /// 将长度值转换为千米
  49. /// </summary>
  50. public double ConvertToKilometer(double value, LengthUnit unit)
  51. {
  52. // Conversion logic here
  53. }
  54. /// <summary>
  55. /// 将重量值转换为克
  56. /// </summary>
  57. public double ConvertToGram(double value, WeightUnit unit)
  58. {
  59. // Conversion logic here
  60. }
  61. /// <summary>
  62. /// 将重量值转换为千克
  63. /// </summary>
  64. public double ConvertToKilogram(double value, WeightUnit unit)
  65. {
  66. // Conversion logic here
  67. }
  68. /// <summary>
  69. /// 将功率值转换为瓦特
  70. /// </summary>
  71. public double ConvertToWatt(double value, PowerUnit unit)
  72. {
  73. // Conversion logic here
  74. }
  75. /// <summary>
  76. /// 将电流值转换为安培
  77. /// </summary>
  78. public double ConvertToAmpere(double value, CurrentUnit unit)
  79. {
  80. // Conversion logic here
  81. }
  82. /// <summary>
  83. /// 将电压值转换为伏特
  84. /// </summary>
  85. public double ConvertToVolt(double value, VoltageUnit unit)
  86. {
  87. // Conversion logic here
  88. }
  89. }

这里你会发现,其他方法未实现,需要继续告诉Github Copilot继续生成代码

Prompt:MeasurementService类中,请参考ConvertToInch这个方法的实现方式和注释要求,继续生成ConvertToMile,ConvertToFoot,ConvertToCentimeter,ConvertToKilometer,ConvertToGram,ConvertToKilogram,ConvertToWatt,ConvertToAmpere,ConvertToVolt等方法的具体转换逻辑,每个方法都要实现代码输出

Github Copilot生成的代码不一定全面准确,需要做代码确认,这个很关键,以下是人工确认后的代码(改了不少地方)

  1. /// <summary>
  2. /// 提供度量衡服务的实现类
  3. /// </summary>
  4. public class MeasurementService : IMeasurementService
  5. {
  6. /// <summary>
  7. /// 将长度值转换为英寸
  8. /// </summary>
  9. public double ConvertToInch(double value, LengthUnit lengthUnit)
  10. {
  11. switch (lengthUnit)
  12. {
  13. case LengthUnit.Inch:
  14. return value;
  15. case LengthUnit.Meter:
  16. return value * 39.3701;
  17. case LengthUnit.Centimeter:
  18. return value * 0.393701;
  19. case LengthUnit.Kilometer:
  20. return value * 39370.1;
  21. case LengthUnit.Mile:
  22. return value * 63360;
  23. case LengthUnit.Foot:
  24. return value * 12;
  25. case LengthUnit.Yard:
  26. return value * 36;
  27. default:
  28. throw new NotSupportedException("The provided LengthUnit is not supported.");
  29. }
  30. }
  31. /// <summary>
  32. /// 将长度值转换为英里
  33. /// </summary>
  34. public double ConvertToMile(double value, LengthUnit lengthUnit)
  35. {
  36. // 根据长度单位进行转换
  37. switch (lengthUnit)
  38. {
  39. case LengthUnit.Mile:
  40. return value;
  41. case LengthUnit.Centimeter:
  42. return value / 160934.4;
  43. case LengthUnit.Inch:
  44. return value / 63360;
  45. case LengthUnit.Kilometer:
  46. return value / 1.609344;
  47. case LengthUnit.Yard:
  48. return value / 1760;
  49. case LengthUnit.Meter:
  50. return value / 1609.344;
  51. case LengthUnit.Foot:
  52. return value / 5280;
  53. default:
  54. throw new NotSupportedException($"Unsupported length unit: {lengthUnit}");
  55. }
  56. }
  57. /// <summary>
  58. /// 将长度值转换为英尺
  59. /// </summary>
  60. public double ConvertToFoot(double value, LengthUnit lengthUnit)
  61. {
  62. // 根据长度单位进行转换
  63. switch (lengthUnit)
  64. {
  65. case LengthUnit.Foot:
  66. return value;
  67. case LengthUnit.Meter:
  68. return value / 0.3048;
  69. case LengthUnit.Centimeter:
  70. return value / 30.48;
  71. case LengthUnit.Mile:
  72. return value * 5280;
  73. case LengthUnit.Kilometer:
  74. return value * 3280.84;
  75. case LengthUnit.Yard:
  76. return value * 3;
  77. case LengthUnit.Inch:
  78. return value * 12;
  79. default:
  80. throw new NotSupportedException($"Unsupported length unit: {lengthUnit}");
  81. }
  82. }
  83. /// <summary>
  84. /// 将长度值转换为厘米
  85. /// </summary>
  86. public double ConvertToCentimeter(double value, LengthUnit lengthUnit)
  87. {
  88. // 根据长度单位进行转换
  89. switch (lengthUnit)
  90. {
  91. case LengthUnit.Centimeter:
  92. return value;
  93. case LengthUnit.Meter:
  94. return value * 100;
  95. case LengthUnit.Inch:
  96. return value * 2.54;
  97. case LengthUnit.Mile:
  98. return value * 160934.4;
  99. case LengthUnit.Kilometer:
  100. return value * 100000;
  101. case LengthUnit.Yard:
  102. return value * 91.44;
  103. case LengthUnit.Foot:
  104. return value * 30.48;
  105. default:
  106. throw new NotSupportedException($"Unsupported length unit: {lengthUnit}");
  107. }
  108. }
  109. /// <summary>
  110. /// 将长度值转换为千米
  111. /// </summary>
  112. public double ConvertToKilometer(double value, LengthUnit lengthUnit)
  113. {
  114. // 根据长度单位进行转换
  115. switch (lengthUnit)
  116. {
  117. case LengthUnit.Meter:
  118. return value / 1000;
  119. case LengthUnit.Inch:
  120. return value / 39370.1;
  121. case LengthUnit.Mile:
  122. return value / 1.609344;
  123. case LengthUnit.Yard:
  124. return value / 1093.6133;
  125. case LengthUnit.Foot:
  126. return value / 3280.8399;
  127. case LengthUnit.Kilometer:
  128. return value;
  129. case LengthUnit.Centimeter:
  130. return value / 100000;
  131. default:
  132. throw new NotSupportedException($"Unsupported length unit: {lengthUnit}");
  133. }
  134. }
  135. /// <summary>
  136. /// 将重量值转换为克
  137. /// </summary>
  138. public double ConvertToGram(double value, WeightUnit weightUnit)
  139. {
  140. // 根据重量单位进行转换
  141. switch (weightUnit)
  142. {
  143. case WeightUnit.Gram:
  144. return value;
  145. case WeightUnit.Kilogram:
  146. return value * 1000;
  147. case WeightUnit.Pound:
  148. return value * 453.59237;
  149. case WeightUnit.Tonne:
  150. return value * 1000000;
  151. case WeightUnit.Ounce:
  152. return value * 28.349523125;
  153. case WeightUnit.Milligram:
  154. return value / 1000;
  155. default:
  156. throw new NotSupportedException($"Unsupported weight unit: {weightUnit}");
  157. }
  158. }
  159. /// <summary>
  160. /// 将重量值转换为千克
  161. /// </summary>
  162. public double ConvertToKilogram(double value, WeightUnit weightUnit)
  163. {
  164. // 根据重量单位进行转换
  165. switch (weightUnit)
  166. {
  167. case WeightUnit.Kilogram:
  168. return value;
  169. case WeightUnit.Gram:
  170. return value / 1000;
  171. case WeightUnit.Pound:
  172. return value * 0.45359237;
  173. case WeightUnit.Tonne:
  174. return value * 1000;
  175. case WeightUnit.Ounce:
  176. return value * 0.028349523125;
  177. case WeightUnit.Milligram:
  178. return value / 1000000;
  179. default:
  180. throw new NotSupportedException($"Unsupported weight unit: {weightUnit}");
  181. }
  182. }
  183. /// <summary>
  184. /// 将功率值转换为瓦特
  185. /// </summary>
  186. public double ConvertToWatt(double value, PowerUnit powerUnit)
  187. {
  188. // 根据功率单位进行转换
  189. switch (powerUnit)
  190. {
  191. case PowerUnit.Watt:
  192. return value;
  193. case PowerUnit.Kilowatt:
  194. return value * 1000;
  195. case PowerUnit.Horsepower:
  196. return value * 745.699872;
  197. case PowerUnit.Megawatt:
  198. return value * 1000000;
  199. default:
  200. throw new NotSupportedException($"Unsupported power unit: {powerUnit}");
  201. }
  202. }
  203. /// <summary>
  204. /// 将电流值转换为安培
  205. /// </summary>
  206. public double ConvertToAmpere(double value, CurrentUnit currentUnit)
  207. {
  208. // 根据电流单位进行转换
  209. switch (currentUnit)
  210. {
  211. case CurrentUnit.Ampere:
  212. return value;
  213. case CurrentUnit.Milliampere:
  214. return value / 1000;
  215. case CurrentUnit.Microampere:
  216. return value / 1000000;
  217. case CurrentUnit.Kiloampere:
  218. return value * 1000;
  219. default:
  220. throw new NotSupportedException($"Unsupported current unit: {currentUnit}");
  221. }
  222. }
  223. /// <summary>
  224. /// 将电压值转换为伏特
  225. /// </summary>
  226. public double ConvertToVolt(double value, VoltageUnit voltageUnit)
  227. {
  228. // 根据电压单位进行转换
  229. switch (voltageUnit)
  230. {
  231. case VoltageUnit.Volt:
  232. return value;
  233. case VoltageUnit.Millivolt:
  234. return value / 1000;
  235. case VoltageUnit.Microvolt:
  236. return value / 1000000;
  237. case VoltageUnit.Kilovolt:
  238. return value * 1000;
  239. default:
  240. throw new NotSupportedException($"Unsupported voltage unit: {voltageUnit}");
  241. }
  242. }
  243. }

3. 生成单元测试代码

首先选择整个类,然后输入以下Prompt

@workspace /tests  请对选中的代码,使用MSTest单元测试框架,生成单元测试代码,请为每个方法都实现单元测试

 

 每次仅生成了一个方法的单元测试,可以继续使用以下Prompt

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

最终生成的UnitTest代码

  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using T.Core.i10n.Service;
  3. using T.Core.i10n.SPI;
  4. namespace T.Core.i10n.Service.Tests
  5. {
  6. [TestClass]
  7. public class MeasurementServiceTests
  8. {
  9. private MeasurementService? _measurementService;
  10. [TestInitialize]
  11. public void Setup()
  12. {
  13. _measurementService = new MeasurementService();
  14. }
  15. [TestMethod]
  16. public void ConvertToInch_ShouldConvertCorrectly()
  17. {
  18. var result = _measurementService!.ConvertToInch(1, LengthUnit.Meter);
  19. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(39.3701, result, 0.0001);
  20. }
  21. [TestMethod]
  22. [ExpectedException(typeof(NotSupportedException))]
  23. public void ConvertToInch_ShouldThrowExceptionForUnsupportedUnit()
  24. {
  25. _measurementService!.ConvertToInch(1, (LengthUnit)999);
  26. }
  27. [TestMethod]
  28. public void ConvertToMile_ShouldConvertCorrectly()
  29. {
  30. var result = _measurementService!.ConvertToMile(1, LengthUnit.Kilometer);
  31. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.621371, result, 0.0001);
  32. }
  33. [TestMethod]
  34. [ExpectedException(typeof(NotSupportedException))]
  35. public void ConvertToMile_ShouldThrowExceptionForUnsupportedUnit()
  36. {
  37. _measurementService!.ConvertToMile(1, (LengthUnit)999);
  38. }
  39. [TestMethod]
  40. public void ConvertToFoot_ShouldConvertCorrectly()
  41. {
  42. var result = _measurementService!.ConvertToFoot(1, LengthUnit.Meter);
  43. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(3.28084, result, 0.0001);
  44. }
  45. [TestMethod]
  46. [ExpectedException(typeof(NotSupportedException))]
  47. public void ConvertToFoot_ShouldThrowExceptionForUnsupportedUnit()
  48. {
  49. _measurementService!.ConvertToFoot(1, (LengthUnit)999);
  50. }
  51. [TestMethod]
  52. public void ConvertToCentimeter_ShouldConvertCorrectly()
  53. {
  54. var result = _measurementService!.ConvertToCentimeter(1, LengthUnit.Meter);
  55. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(100, result, 0.0001);
  56. }
  57. [TestMethod]
  58. [ExpectedException(typeof(NotSupportedException))]
  59. public void ConvertToCentimeter_ShouldThrowExceptionForUnsupportedUnit()
  60. {
  61. _measurementService!.ConvertToCentimeter(1, (LengthUnit)999);
  62. }
  63. [TestMethod]
  64. public void ConvertToKilometer_ShouldConvertCorrectly()
  65. {
  66. var result = _measurementService!.ConvertToKilometer(1, LengthUnit.Meter);
  67. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, result, 0.0001);
  68. }
  69. [TestMethod]
  70. [ExpectedException(typeof(NotSupportedException))]
  71. public void ConvertToKilometer_ShouldThrowExceptionForUnsupportedUnit()
  72. {
  73. _measurementService!.ConvertToKilometer(1, (LengthUnit)999);
  74. }
  75. [TestMethod]
  76. public void ConvertToGram_ShouldConvertCorrectly()
  77. {
  78. var result = _measurementService!.ConvertToGram(1, WeightUnit.Kilogram);
  79. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, result, 0.0001);
  80. }
  81. [TestMethod]
  82. [ExpectedException(typeof(NotSupportedException))]
  83. public void ConvertToGram_ShouldThrowExceptionForUnsupportedUnit()
  84. {
  85. _measurementService!.ConvertToGram(1, (WeightUnit)999);
  86. }
  87. [TestMethod]
  88. public void ConvertToKilogram_ShouldConvertCorrectly()
  89. {
  90. var result = _measurementService!.ConvertToKilogram(1, WeightUnit.Gram);
  91. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, result, 0.0001);
  92. }
  93. [TestMethod]
  94. [ExpectedException(typeof(NotSupportedException))]
  95. public void ConvertToKilogram_ShouldThrowExceptionForUnsupportedUnit()
  96. {
  97. _measurementService!.ConvertToKilogram(1, (WeightUnit)999);
  98. }
  99. [TestMethod]
  100. public void ConvertToWatt_ShouldConvertCorrectly()
  101. {
  102. var result = _measurementService!.ConvertToWatt(1, PowerUnit.Kilowatt);
  103. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, result, 0.0001);
  104. }
  105. [TestMethod]
  106. [ExpectedException(typeof(NotSupportedException))]
  107. public void ConvertToWatt_ShouldThrowExceptionForUnsupportedUnit()
  108. {
  109. _measurementService!.ConvertToWatt(1, (PowerUnit)999);
  110. }
  111. [TestMethod]
  112. public void ConvertToAmpere_ShouldConvertCorrectly()
  113. {
  114. var result = _measurementService!.ConvertToAmpere(1, CurrentUnit.Milliampere);
  115. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, result, 0.0001);
  116. }
  117. [TestMethod]
  118. [ExpectedException(typeof(NotSupportedException))]
  119. public void ConvertToAmpere_ShouldThrowExceptionForUnsupportedUnit()
  120. {
  121. _measurementService!.ConvertToAmpere(1, (CurrentUnit)999);
  122. }
  123. [TestMethod]
  124. public void ConvertToVolt_ShouldConvertCorrectly()
  125. {
  126. var result = _measurementService!.ConvertToVolt(1, VoltageUnit.Millivolt);
  127. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, result, 0.0001);
  128. }
  129. [TestMethod]
  130. [ExpectedException(typeof(NotSupportedException))]
  131. public void ConvertToVolt_ShouldThrowExceptionForUnsupportedUnit()
  132. {
  133. _measurementService!.ConvertToVolt(1, (VoltageUnit)999);
  134. }
  135. [TestMethod]
  136. public void ConvertToInch_ShouldConvertCorrectly_ForEachLengthUnit()
  137. {
  138. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToInch(1, LengthUnit.Inch), 0.0001);
  139. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(39.3701, _measurementService!.ConvertToInch(1, LengthUnit.Meter), 0.0001);
  140. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.393701, _measurementService!.ConvertToInch(1, LengthUnit.Centimeter), 0.0001);
  141. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(39370.1, _measurementService!.ConvertToInch(1, LengthUnit.Kilometer), 0.0001);
  142. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(63360, _measurementService!.ConvertToInch(1, LengthUnit.Mile), 0.0001);
  143. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(12, _measurementService!.ConvertToInch(1, LengthUnit.Foot), 0.0001);
  144. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(36, _measurementService!.ConvertToInch(1, LengthUnit.Yard), 0.0001);
  145. }
  146. [TestMethod]
  147. public void ConvertToMile_ShouldConvertCorrectly_ForEachLengthUnit()
  148. {
  149. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToMile(1, LengthUnit.Mile), 0.0001);
  150. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0000062137, _measurementService!.ConvertToMile(1, LengthUnit.Centimeter), 0.0000000001);
  151. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0000157828, _measurementService!.ConvertToMile(1, LengthUnit.Inch), 0.0000000001);
  152. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.621371, _measurementService!.ConvertToMile(1, LengthUnit.Kilometer), 0.0001);
  153. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0005681818, _measurementService!.ConvertToMile(1, LengthUnit.Yard), 0.0000000001);
  154. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0006213712, _measurementService!.ConvertToMile(1, LengthUnit.Meter), 0.0000000001);
  155. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0001893939, _measurementService!.ConvertToMile(1, LengthUnit.Foot), 0.0000000001);
  156. }
  157. [TestMethod]
  158. public void ConvertToFoot_ShouldConvertCorrectly_ForEachLengthUnit()
  159. {
  160. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToFoot(1, LengthUnit.Foot), 0.0001);
  161. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(3.28084, _measurementService!.ConvertToFoot(1, LengthUnit.Meter), 0.0001);
  162. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0328084, _measurementService!.ConvertToFoot(1, LengthUnit.Centimeter), 0.0001);
  163. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(3280.84, _measurementService!.ConvertToFoot(1, LengthUnit.Kilometer), 0.0001);
  164. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(5280, _measurementService!.ConvertToFoot(1, LengthUnit.Mile), 0.0001);
  165. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(3, _measurementService!.ConvertToFoot(1, LengthUnit.Yard), 0.0001);
  166. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0833333, _measurementService!.ConvertToFoot(1, LengthUnit.Inch), 0.0001);
  167. }
  168. [TestMethod]
  169. public void ConvertToCentimeter_ShouldConvertCorrectly_ForEachLengthUnit()
  170. {
  171. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToCentimeter(1, LengthUnit.Centimeter), 0.0001);
  172. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(100, _measurementService!.ConvertToCentimeter(1, LengthUnit.Meter), 0.0001);
  173. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(2.54, _measurementService!.ConvertToCentimeter(1, LengthUnit.Inch), 0.0001);
  174. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(100000, _measurementService!.ConvertToCentimeter(1, LengthUnit.Kilometer), 0.0001);
  175. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(160934.4, _measurementService!.ConvertToCentimeter(1, LengthUnit.Mile), 0.0001);
  176. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(91.44, _measurementService!.ConvertToCentimeter(1, LengthUnit.Yard), 0.0001);
  177. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(30.48, _measurementService!.ConvertToCentimeter(1, LengthUnit.Foot), 0.0001);
  178. }
  179. // Continue with similar tests for the other methods...
  180. [TestMethod]
  181. public void ConvertToKilometer_ShouldConvertCorrectly_ForEachLengthUnit()
  182. {
  183. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToKilometer(1, LengthUnit.Kilometer), 0.0001);
  184. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, _measurementService!.ConvertToKilometer(1, LengthUnit.Meter), 0.0001);
  185. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0000254, _measurementService!.ConvertToKilometer(1, LengthUnit.Inch), 0.0001);
  186. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1.609344, _measurementService!.ConvertToKilometer(1, LengthUnit.Mile), 0.0001);
  187. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0009144, _measurementService!.ConvertToKilometer(1, LengthUnit.Yard), 0.0001);
  188. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0003048, _measurementService!.ConvertToKilometer(1, LengthUnit.Foot), 0.0001);
  189. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.00001, _measurementService!.ConvertToKilometer(1, LengthUnit.Centimeter), 0.0001);
  190. }
  191. [TestMethod]
  192. public void ConvertToGram_ShouldConvertCorrectly_ForEachWeightUnit()
  193. {
  194. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToGram(1, WeightUnit.Gram), 0.0001);
  195. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, _measurementService!.ConvertToGram(1, WeightUnit.Kilogram), 0.0001);
  196. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(453.59237, _measurementService!.ConvertToGram(1, WeightUnit.Pound), 0.0001);
  197. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000000, _measurementService!.ConvertToGram(1, WeightUnit.Tonne), 0.0001);
  198. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(28.349523125, _measurementService!.ConvertToGram(1, WeightUnit.Ounce), 0.0001);
  199. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, _measurementService!.ConvertToGram(1, WeightUnit.Milligram), 0.0001);
  200. }
  201. [TestMethod]
  202. public void ConvertToKilogram_ShouldConvertCorrectly_ForEachWeightUnit()
  203. {
  204. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToKilogram(1, WeightUnit.Kilogram), 0.0001);
  205. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, _measurementService!.ConvertToKilogram(1, WeightUnit.Gram), 0.0001);
  206. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.453592, _measurementService!.ConvertToKilogram(1, WeightUnit.Pound), 0.0001);
  207. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, _measurementService!.ConvertToKilogram(1, WeightUnit.Tonne), 0.0001);
  208. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.0283495, _measurementService!.ConvertToKilogram(1, WeightUnit.Ounce), 0.0001);
  209. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.000001, _measurementService!.ConvertToKilogram(1, WeightUnit.Milligram), 0.0001);
  210. }
  211. [TestMethod]
  212. public void ConvertToWatt_ShouldConvertCorrectly_ForEachPowerUnit()
  213. {
  214. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToWatt(1, PowerUnit.Watt), 0.0001);
  215. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, _measurementService!.ConvertToWatt(1, PowerUnit.Kilowatt), 0.0001);
  216. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(745.7, _measurementService!.ConvertToWatt(1, PowerUnit.Horsepower), 0.0001);
  217. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000000, _measurementService!.ConvertToWatt(1, PowerUnit.Megawatt), 0.0001);
  218. }
  219. [TestMethod]
  220. public void ConvertToAmpere_ShouldConvertCorrectly_ForEachCurrentUnit()
  221. {
  222. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToAmpere(1, CurrentUnit.Ampere), 0.0001);
  223. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, _measurementService!.ConvertToAmpere(1, CurrentUnit.Milliampere), 0.0001);
  224. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.000001, _measurementService!.ConvertToAmpere(1, CurrentUnit.Microampere), 0.0001);
  225. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, _measurementService!.ConvertToAmpere(1, CurrentUnit.Kiloampere), 0.0001);
  226. }
  227. [TestMethod]
  228. public void ConvertToVolt_ShouldConvertCorrectly_ForEachVoltageUnit()
  229. {
  230. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1, _measurementService!.ConvertToVolt(1, VoltageUnit.Volt), 0.0001);
  231. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.001, _measurementService!.ConvertToVolt(1, VoltageUnit.Millivolt), 0.0001);
  232. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(0.000001, _measurementService!.ConvertToVolt(1, VoltageUnit.Microvolt), 0.0001);
  233. Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(1000, _measurementService!.ConvertToVolt(1, VoltageUnit.Kilovolt), 0.0001);
  234. }
  235. // Continue with similar tests for the other methods...
  236. }
  237. }

以上单元测试代码,直接在VS Code 右键Run Test即可。

 

 

周国庆

2023/11/5

 

原文链接:https://www.cnblogs.com/tianqing/p/17810231.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号