如果您正在编写代码,为什么不将其表示为代码呢?诚然,在 XML 文档中包含示例代码是相当常见的,但这很少以测试的形式出现 - 并且如果没有等效的“交互式会话”,则需要人工构造来以可测试的形式表示输出。
我并不是说这是 Python 中的一个不好的特性 - 只是我认为它不能很好地映射到 C# 中。语言有自己的风格,并不是 X 语言中的每个功能在 Y 语言中都有意义。
Well for one thing, the documentation for doctest talks about "interactive Python sessions". There's no equivalent of that in C#... so how would the output be represented? How would you perform all the necessary setup?
I dare say such a thing would be possible, but personally I think that at least for C#, it's clearer to have unit tests as unit tests, where you have all the benefits of the fact that you're writing code rather than comments. The code can be checked for syntactic correctness at compile-time, you have IntelliSense, syntax highlighting, debugger support etc.
If you're writing code, why not represent that as code? Admittedly it's reasonably common to include sample code in XML documentation, but that's rarely in the form of tests - and without an equivalent of an "interactive session" it would require an artificial construct to represent the output in a testable form.
I'm not saying this is a bad feature in Python - just that it's one which I don't believe maps over to C# particularly well. Languages have their own styles, and not every feature in language X will make sense in language Y.
发布评论
评论(1)
一方面,doctest 文档讨论了“交互式 Python 会话”。 C# 中没有类似的东西...那么输出将如何表示呢?您将如何执行所有必要的设置?
我敢说这样的事情是可能的,但我个人认为,至少对于 C# 来说,将单元测试作为单元测试会更清楚,在这种情况下,您可以享受到以下事实的所有好处:编写代码而不是注释。可以在编译时检查代码的语法正确性,您有 IntelliSense、语法突出显示、调试器支持等。
如果您正在编写代码,为什么不将其表示为代码呢?诚然,在 XML 文档中包含示例代码是相当常见的,但这很少以测试的形式出现 - 并且如果没有等效的“交互式会话”,则需要人工构造来以可测试的形式表示输出。
我并不是说这是 Python 中的一个不好的特性 - 只是我认为它不能很好地映射到 C# 中。语言有自己的风格,并不是 X 语言中的每个功能在 Y 语言中都有意义。
Well for one thing, the documentation for doctest talks about "interactive Python sessions". There's no equivalent of that in C#... so how would the output be represented? How would you perform all the necessary setup?
I dare say such a thing would be possible, but personally I think that at least for C#, it's clearer to have unit tests as unit tests, where you have all the benefits of the fact that you're writing code rather than comments. The code can be checked for syntactic correctness at compile-time, you have IntelliSense, syntax highlighting, debugger support etc.
If you're writing code, why not represent that as code? Admittedly it's reasonably common to include sample code in XML documentation, but that's rarely in the form of tests - and without an equivalent of an "interactive session" it would require an artificial construct to represent the output in a testable form.
I'm not saying this is a bad feature in Python - just that it's one which I don't believe maps over to C# particularly well. Languages have their own styles, and not every feature in language X will make sense in language Y.