使用类的main方法进行调试?

发布于 2024-07-07 17:26:50 字数 179 浏览 6 评论 0原文

使用 main 方法来测试 java/.net 类是个好习惯吗?

我在一些教科书上看到过推荐它,但对我来说,使用单元测试框架似乎更有意义......

main 方法为您提供了进入该类的一个入口点,您可以测试该类的一个方面功能。 我猜你可以测试很多,但它似乎没有使用 Junit 或 Nunit 有意义。

It is good practice to use the main method to test a java/.net class?

I've seen it reccommended in some text books, but to me it seems like using a unit testing framework would make more sense...

The main method gives you one point of entry to the class and you can test one aspect of the classes functionality. You could I guess test many but it doesn't seem to make sense as much as using Junit or Nunit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

朮生 2024-07-14 17:26:50

一个明显的优点似乎是您可以对类进行白盒测试。 也就是说,您可以测试它的内部结构(例如私有方法)。 您不能通过单元测试来做到这一点,您也不想这样做,它们主要是从用户的角度测试界面和行为。

One obvious advantage seems to be that you can whitebox test the class. That is, you can test the internals of it (private methods for example). You can't do that with unit-tests, nor would you wan't that, they are primarily there to test the interface and the behavior from the users perspective.

戏剧牡丹亭 2024-07-14 17:26:50

我认为开发从测试集成测试套件的主方法(如测试运行程序)调用的集成测试可能很有用。 我不会以这种方式进行单元测试,因为单元测试框架提供了更好的机制来执行此操作。

[编辑] 为了澄清,我并不是建议每个类都有用于集成测试的静态 main 方法,而是建议您可以使用静态 main 方法编写一个集成测试程序来运行您的集成测试套件。

I think it could be useful to develop integration tests that are invoked from a main method -- like a test runner -- that tests suites of integration tests. I wouldn't do unit testing this way as unit testing frameworks provide a much better mechanism to do this.

[EDIT] To clarify, I'm not suggesting that each class have static main method to be used for integration tests, but rather that you could write an integration test program with a static main method that would run your suite of integration tests.

沉鱼一梦 2024-07-14 17:26:50

main 方法对于某些情况可能很有用,但使用调试器然后编写单元测试(以提供一些针对回归的保险)是一个更强大的解决方案。

The main method can be useful for certain situations, but using a debugger and then writing a unit test (to provide some insurance against regressions) is a more robust solution.

爱你不解释 2024-07-14 17:26:50

在 Java 中,可以接受多个 main 方法并使用它们进行测试,但是 .NET 不允许这样做,如果您在同一个程序中有两个 main 方法,您将得到 编译器错误 CS0017,并告诉您使用 /main 进行编译以指定包含入口点的类型。

我不得不说 Java 方式对我来说更有意义。

In Java it's accepted to have multiple main methods and use them for testing however .NET doesn't allow this, if you have two mains in the same program you will get compiler error CS0017, and tells you to Compile with /main to specify the type that contains the entry point.

I've got to say the Java way makes more sense to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文