使用TestContainers提高测试性能
生活随笔
收集整理的這篇文章主要介紹了
使用TestContainers提高测试性能
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在我以前的測試文章中,我描述了如何使用TestContainers為數(shù)據(jù)庫測試提供現(xiàn)實的測試環(huán)境。 此評論顯示了缺點:
…如上所述,似乎總是有一些缺點。 在這種情況下,啟動Docker映像及其包含的所有內(nèi)容的開銷將增加您的總體構(gòu)建時間。
提醒一下,這是TestContainer特定的代碼。 注意實例成員postgres ,以及根據(jù)每個方法重新初始化它的JUnit Rule 。
package be.objectify.tcexample.db;import be.objectify.tcexample.AbstractUserDaoTest; import be.objectify.tcexample.UserDao; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.testcontainers.containers.PostgreSQLContainer; import play.db.Database;public class JooqUserDaoTest extends AbstractUserDaoTest implements DbTestSupport,TestData {@Rulepublic PostgreSQLContainer postgres = new PostgreSQLContainer();private Database database;@Beforepublic void setup() throws Exception {// the database has all evolutions applieddatabase = create(postgres); // load some test dataloadTestData(database); }@Afterpublic void tearDown() {destroy(database);}@Overridepublic UserDao dao() {return new JooqUserDao(database);} }鑒于測試持續(xù)時間的巨大增加是由Docker容器啟動時間導(dǎo)致的,因此我們可以改用JUnit ClassRule啟動一個容器,并將其重新用于類中的每個測試。 這意味著您不再應(yīng)該并行運行這些測試,但是性能提升將大大超過測試并行化。
package be.objectify.tcexample.db;import be.objectify.tcexample.AbstractUserDaoTest; import be.objectify.tcexample.UserDao; import org.junit.After; import org.junit.Before; import org.junit.ClassRule; import org.testcontainers.containers.PostgreSQLContainer; import play.db.Database;public class FasterJooqUserDaoTest extends AbstractUserDaoTest implements DbTestSupport,TestData {@ClassRulepublic static PostgreSQLContainer postgres = new PostgreSQLContainer();private Database database;@Beforepublic void setup() throws Exception {database = create(postgres); loadTestData(database); }@Afterpublic void tearDown() {destroy(database);}@Overridepublic UserDao dao() {return new JooqUserDao(database);} }節(jié)省的時間取決于類中測試方法的數(shù)量。 我有一些測試類,每個類最多包含30個測試,在這種情況下,執(zhí)行時間從幾分鐘縮短到幾秒鐘。 更改幾行代碼也不錯。
翻譯自: https://www.javacodegeeks.com/2017/05/boosting-test-performance-testcontainers.html
總結(jié)
以上是生活随笔為你收集整理的使用TestContainers提高测试性能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于电脑配置如何看笔记本电脑配置好坏
- 下一篇: 苹果电脑如何连接网线苹果笔记本电脑如何连