日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

测试案例6种编写方法_一种编写测试的好方法

發(fā)布時(shí)間:2023/12/3 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 测试案例6种编写方法_一种编写测试的好方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

測試案例6種編寫方法

測試。 我最近一直在考慮測試。 作為我對各種項(xiàng)目所做的代碼審查的一部分,我已經(jīng)看到了數(shù)千行未經(jīng)測試的代碼。 這不僅是測試覆蓋率統(tǒng)計(jì)數(shù)據(jù)指出這一點(diǎn)的情況,更是該項(xiàng)目中根本沒有任何測試的情況 。 我一直聽到這種悲慘狀況的兩個(gè)原因是什么? “我們沒有時(shí)間”,緊隨其后的是“完成代碼后就去做”。

我在這里介紹的不是萬能藥。 它涵蓋了單元測試,尤其是接口的單元測試。 接口是好東西。 接口定義合同。 接口,無論接口有多少種實(shí)現(xiàn),都可以輕松,輕松地進(jìn)行測試。 讓我們看看如何使用此類結(jié)構(gòu)作為示例。

CustomerService是我們的界面。 為了使示例保持簡單,它有兩種方法,下面將進(jìn)行介紹。 請注意javadoc-這是描述合同的地方。

public interface CustomerService {/*** Retrieve the customer from somewhere.* @param userName the userName of the customer* @return a non-null Customer instance compliant with the userName* @throws CustomerNotFoundException if a customer with the given user name can not be found*/Customer get(String userName) throws CustomerNotFoundException;/*** Persist the customer.* @param customer the customer to persist* @return the customer as it now exists in its persisted form* @throws DuplicateCustomerException if a customer with the user name already exists*/Customer create(Customer customer) throws DuplicateCustomerException; }

從圖中可以看到,我們有兩個(gè)此類的實(shí)現(xiàn),RemoteCustomerService和CachingCustomerService。 這些的實(shí)現(xiàn)未顯示,因?yàn)樗鼈儫o關(guān)緊要。 我怎么說呢 很簡單–我們正在測試合同。 我們?yōu)榻涌谥械拿總€(gè)方法以及合約的每個(gè)排列編寫測試。 例如,對于get(),我們需要測試存在具有給定用戶名的客戶時(shí)發(fā)生的情況,以及不存在時(shí)發(fā)生的情況。

public abstract class CustomerServiceTest {@Testpublic void testCreate(){CustomerService customerService = getCustomerService();Customer customer = customerService.create(new Customer("userNameA"));Assert.assertNotNull(customer);Assert.assertEquals("userNameA",customer.getUserName());}@Test(expected = DuplicateCustomerException.class)public void testCreate_duplicate(){CustomerService customerService = getCustomerService();Customer customer = new Customer("userNameA");customerService.create(customer);customerService.create(customer);}@Testpublic void testGet(){CustomerService customerService = getCustomerService();customerService.create(new Customer("userNameA"));Customer customer = customerService.get("userNameA");Assert.assertNotNull(customer);Assert.assertEquals("userNameA",result.getUserName());}@Test(expected = CustomerNotFoundException.class)public void testGet_noUser(){CustomerService customerService = getCustomerService();customerService.get("userNameA");}public abstract CustomerService getCustomerService(); }

現(xiàn)在,我們對合同進(jìn)行了測試,并且我們從未提及任何實(shí)現(xiàn)。 這意味著兩件事:

  • 我們不需要為每個(gè)實(shí)現(xiàn)重復(fù)測試。 這是一件非常好的事情。
  • 沒有一個(gè)實(shí)現(xiàn)正在測試中。 我們可以通過為每個(gè)實(shí)現(xiàn)添加一個(gè)測試類來糾正此問題。 由于每個(gè)測試類幾乎都是相同的,因此我將僅演示RemoteCustomerService的測試。
public class RemoteCustomerServiceTest extends CustomerServiceTest {public CustomerService getCustomerService(){return new RemoteCustomerService();} }

就是這樣! 現(xiàn)在,我們有了一種非常簡單的方法來測試任何接口的多個(gè)實(shí)現(xiàn),方法是預(yù)先進(jìn)行艱苦的工作,并將測試新實(shí)現(xiàn)的工作減少到一個(gè)簡單的方法中。

參考: Objective博客上的JCG合作伙伴 Steve Chaloner 編寫測試的一種好方法 。

翻譯自: https://www.javacodegeeks.com/2013/06/a-good-lazy-way-to-write-tests.html

測試案例6種編寫方法

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的测试案例6种编写方法_一种编写测试的好方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。