日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用TestContainers提高测试性能

發布時間:2023/12/3 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用TestContainers提高测试性能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在我以前的測試文章中,我描述了如何使用TestContainers為數據庫測試提供現實的測試環境。 此評論顯示了缺點:

…如上所述,似乎總是有一些缺點。 在這種情況下,啟動Docker映像及其包含的所有內容的開銷將增加您的總體構建時間。

提醒一下,這是TestContainer特定的代碼。 注意實例成員postgres ,以及根據每個方法重新初始化它的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);} }

鑒于測試持續時間的巨大增加是由Docker容器啟動時間導致的,因此我們可以改用JUnit ClassRule啟動一個容器,并將其重新用于類中的每個測試。 這意味著您不再應該并行運行這些測試,但是性能提升將大大超過測試并行化。

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);} }

節省的時間取決于類中測試方法的數量。 我有一些測試類,每個類最多包含30個測試,在這種情況下,執行時間從幾分鐘縮短到幾秒鐘。 更改幾行代碼也不錯。

翻譯自: https://www.javacodegeeks.com/2017/05/boosting-test-performance-testcontainers.html

總結

以上是生活随笔為你收集整理的使用TestContainers提高测试性能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。