我的团队负责为我们也编写的大型系统开发 API。我们需要提供示例代码,以便其他使用我们 API 的开发人员可以学习如何使用它。我们一直在使用 xml 文档注释来记录代码。
例如。
/// <summary>Summary here</summary>
/// <example>Here is an example <code>example code here</code> </example>
public void SomeFunction()
然后我们使用 SandcaSTLe 并构建我们需要的帮助文件(chm 和一个在线网站)。
当示例代码不起作用时,这是非常尴尬的,这通常是因为某些功能发生了变化或一个简单的错误。
有没有人做过这样的事情,但还配置了单元测试以在示例代码上运行,以便在构建期间知道它们可以工作?
最佳答案
是的,sandcaSTLe 支持这一点,并且很好地保持示例的正确性。您可以像这样指向代码区域:
/// <summary>
/// Gizmo which can act as client or server.
/// </summary>
/// <example>
/// The following example shows how to use the gizmo as a client:
/// <code lang="cs"
/// source="..\gizmo.unittests\TestGizmo.cs"
/// region="GizmoClientSample"/>
/// </example>
public class Gizmo
然后,您可以使用 TestGizmo.cs 中的一些测试代码作为示例,将其包含在一个区域中:
[Test]
public GizmoCanActAsClient()
{
#region GizmoClientSample
Gizmo gizmo = new Gizmo();
gizmo.ActAsClient();
#endregion
}
警告:如果您移动或重命名测试文件,您只会在尝试使用 sandcaSTLe 重新生成文档时收到关于此的错误。
关于.net - 自动单元测试示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/301365/