简体   繁体   English

为什么我的JUnit测试用例在作为Test Suite和单个Junit文件运行时会显示不同的输出

[英]Why my JUnit test cases exhibit different output when run as a Test Suite and individual Junit file

I have a set of JUnit test files and also a test suite (Suite class) file which holds the reference to all individual Junit test files. 我有一组JUnit测试文件以及一个测试套件(Suite类)文件,该文件包含对所有单个Junit测试文件的引用。

All are database oriented. 都是面向数据库的。 The database used is Mysql and I am using Eclipse IDE to run the tests. 使用的数据库是Mysql,我使用Eclipse IDE来运行测试。

When running each file individually I get the correct value and the assertion is correct but when running from the test suite it shows a different value. 当单独运行每个文件时,我得到正确的值并且断言是正确的但是当从测试套件运行时它显示不同的值。

I have made each JUnit test file to access the database independently with a different database name (even though the table structure is same) 我已经使每个JUnit测试文件独立地使用不同的数据库名称访问数据库(即使表结构相同)

Whether we need to prevent the JUnit test cases from running parallel / the database related statements need to be verified ? 我们是否需要防止JUnit测试用例并行运行/数据库相关语句需要验证?

I would suggest you look into : 我建议你研究一下:

  • any static data (a singleton, static methods, etc...) 任何静态数据(单例,静态方法等......)
  • the order of tests (is it possible that some of the tests depends on some data that another test create, erase, or modify ?) 测试的顺序(某些测试是否可能依赖于另一个测试创建,擦除或修改的某些数据?)
  • if you're using a framework that allow suite-wide setUp or tearDown methods, could they somehow break individual tests ? 如果您使用的是允许套件范围的setUp或tearDown方法的框架,它们是否会以某种方式破坏单个测试?
  • since you're using a database, is it possible that somehow your code is transactionnal, but when running the tests in a Suite some transactions are not commited at the right time (for example, not before the whole suite is finished) ? 既然您正在使用数据库,那么您的代码是否可能是事务性的,但是在套件中运行测试时,某些事务不会在正确的时间提交(例如,不是在整个套件完成之前)?

There are 2 reasons that this may happen. 这可能有两个原因。

One is that you have not constructed the suite properly and some tests share resources with others. 一个是你没有正确构建套件,有些测试与其他测试共享资源。

And the other is that when you finish a test, you don't rollback the database and so the next test finds database in an erroneous state. 另一个是当你完成测试时,你不回滚数据库,所以下一个测试发现数据库处于错误状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM