使用Junit和Easytest进行数据驱动的测试
在本文中,我們將看到如何使用Junit進(jìn)行數(shù)據(jù)驅(qū)動(dòng)的測(cè)試。 為此,我將使用一個(gè)名為EasyTest的庫(kù)。
眾所周知,對(duì)于TestNG,它已內(nèi)置了數(shù)據(jù)提供程序。 通過(guò)簡(jiǎn)單的測(cè)試,我們可以使用Junit進(jìn)行數(shù)據(jù)驅(qū)動(dòng)的測(cè)試。
什么是數(shù)據(jù)驅(qū)動(dòng)測(cè)試?
當(dāng)測(cè)試由數(shù)據(jù)驅(qū)動(dòng)時(shí),則指的是數(shù)據(jù)驅(qū)動(dòng)的測(cè)試。 正式定義可以在Wiki中找到。
總之,將對(duì)輸入數(shù)據(jù),預(yù)期輸出,配置等進(jìn)行參數(shù)化定義。 因此,最后,您不需要更改測(cè)試,但是隨著數(shù)據(jù)的更改,您可以增加測(cè)試數(shù)量和覆蓋范圍。 這意味著,您的數(shù)據(jù)將驅(qū)動(dòng)您的測(cè)試能力以及質(zhì)量。
當(dāng)您需要處理具有不同排列和組合的大量數(shù)據(jù)時(shí),這是一種非常好的方法。
我在哪里使用這種方法?
1.當(dāng)我需要檢查大量數(shù)據(jù)并將其針對(duì)數(shù)據(jù)庫(kù)查詢或Web api方法(REST / SOAP)輸出時(shí)。
2.當(dāng)我需要使用不同的數(shù)據(jù)組合來(lái)驅(qū)動(dòng)相同的UI測(cè)試時(shí)。
3.當(dāng)我需要將日期更改行為與配置更改隔離開(kāi)來(lái)時(shí)。
我們將如何實(shí)現(xiàn)?
我們將通過(guò)參數(shù)化測(cè)試解決此問(wèn)題。 并且此參數(shù)將從我們定義的文件中獲取值(測(cè)試數(shù)據(jù))。 這是TestNG與Junit一起缺少的部分。 我們將使用EasyTest庫(kù)解決。
注意:這不是我們使用Easy test的唯一原因。 簡(jiǎn)易測(cè)試還具有許多令人印象深刻的功能。 我們將一一看到。 這是github中的簡(jiǎn)單測(cè)試項(xiàng)目鏈接。
讓我們學(xué)習(xí)Example:
為了學(xué)習(xí),我正在使用簡(jiǎn)單的計(jì)算器類( Github鏈接 )。 只需將兩個(gè)數(shù)字相加即可得到Double類型的結(jié)果。
public class Calculator {public Double add(Double a, Double b){return a+b;} }而且,讓我們?cè)跊](méi)有Easy Test的情況下創(chuàng)建測(cè)試用例。
public class SimpleTest extends DataDrivenTest{@Test public void testAddition(){Assert.assertEquals(25.5,calculator.add(10.5,15.0),0.01);} }我們將使用Easy Test來(lái)開(kāi)發(fā)這個(gè)簡(jiǎn)單的測(cè)試用例,因此讓我們開(kāi)始制作一個(gè)項(xiàng)目。
步驟A:創(chuàng)建Maven項(xiàng)目:
1.使用您最喜歡的組ID和工件ID創(chuàng)建一個(gè)Maven項(xiàng)目。 (我使用org.automation和數(shù)據(jù)驅(qū)動(dòng))
2.包括以下依賴項(xiàng)。
對(duì)于Junit(我們正在使用)
dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version> </dependency>輕松測(cè)試
<dependency><groupId>org.easetech</groupId><artifactId>easytest-core</artifactId><version>1.4.0</version> </dependency>并且用于日志記錄(這是可選的)
<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.21</version> </dependency> <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.21</version> </dependency>[這足以使用簡(jiǎn)單的測(cè)試內(nèi)部日志記錄。
]
現(xiàn)在,要提供數(shù)據(jù)參數(shù),我們需要包括數(shù)據(jù)文件。 作為最佳實(shí)踐,我會(huì)將所有數(shù)據(jù)都用作資源 。 因此,我需要在pom中添加資源。 因此,最終項(xiàng)目pom.xml看起來(lái)像這樣 。
<?xml version="1.0" encoding="UTF-8"?><project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.automation</groupId><artifactId>datadriven</artifactId><version>1.0-SNAPSHOT</version><dependencies><!--Only mandatory part : start--><dependency><groupId>org.easetech</groupId><artifactId>easytest-core</artifactId><version>1.4.0</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><!--Only mandatory part: End --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.21</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.21</version></dependency></dependencies><build><resources><resource><directory>src/test/resources</directory><includes><include>**/*</include></includes></resource></resources></build> </project>步驟B:創(chuàng)建數(shù)據(jù)文件:簡(jiǎn)易測(cè)試支持
1. Excel:Office 97/2003格式(xls擴(kuò)展名)
2. CSV:逗號(hào)分隔值
3. XML
4.通過(guò)CSV文件的JSON格式。
注意:Easy test還支持自定義數(shù)據(jù)類型(我們將跳過(guò)此部分以使其在Blog中變得簡(jiǎn)單,您可以在Github中查看示例)
對(duì)于CSV和EXCEL,請(qǐng)遵循以下規(guī)則
1.第一行,第一列為方法名稱(因此,此列的每一行將為空白)
2.第一行從第二列開(kāi)始,所有將均為參數(shù)變量名稱。
3.寫(xiě)入方法名稱的列的所有行均為空白。
CSV:
Excel(.xls):
XML:
您可以在github中看到我所有的文件。
現(xiàn)在,讓我們從其他數(shù)據(jù)加載器加載數(shù)據(jù)。
要加載數(shù)據(jù),通常我們使用注釋
1. @DataLoader定義文件源。
2. @Param定義將哪些列數(shù)據(jù)視為要獲取的項(xiàng)目。
由于此批注特定于Easy Test,因此我們需要使用DataDrivenTestRunner.class來(lái)運(yùn)行此測(cè)試類,該類由
因此,首先我們看到CSV數(shù)據(jù)加載器。
@RunWith(DataDrivenTestRunner.class) public class CSVLoaderExample extends DataDrivenTest {@Test @DataLoader(filePaths = "calculator.csv", loaderType = LoaderType.CSV)public String testAddFromCSV(@Param(name = "a") Double a,@Param(name = "b") Double b,@Param(name = "expected") Double expected) {Assert.assertEquals(expected, calculator.add(a, b), 0.1);return "success";}@Test @DataLoader(filePaths = "calculator2.csv")public void testAdd(@Param(name = "a") Double a, @Param(name = "b")Double b, @Param(name = "expected")Double expected){Assert.assertEquals(expected, calculator.add(a,b),0.1);} }在這里,您可以看到,我是
=>使用DataDrivenTestRunner.class運(yùn)行測(cè)試
=>正在加載calculator.csv(還有另外一個(gè))
=>獲取名稱為a,b的參數(shù)。
這是CSV文件的內(nèi)容,看起來(lái)像
testAddFromCSV,a,b,expected,15.0,25.0,40,15.0,25.0,40,15.0,25.0,40,15.0,25.0,40,15.0,25.0,40,15.0,25.0,40,15.0,25.0,40,15.0,25.0,40,15.0,25.0,40,15.0,25.0,40,900.0,250.0,1150.0現(xiàn)在,您可能會(huì)問(wèn)如何處理行。 輕松測(cè)試將每一行視為一條記錄,它將根據(jù)數(shù)據(jù)文件中存在的行數(shù)來(lái)迭代我們的測(cè)試。 因此,為輸入數(shù)據(jù)定義列已綽綽有余。
最佳實(shí)踐:我過(guò)去一直遵循一個(gè)文件來(lái)提供輸入數(shù)據(jù)以及預(yù)期的輸出。 您可以為此使用單獨(dú)的文件。
同樣,如果我們查看excel數(shù)據(jù)加載器:
@RunWith(DataDrivenTestRunner.class) public class ExcelLoaderExample extends DataDrivenTest {@Test @DataLoader(filePaths = "calculator.xls", loaderType = LoaderType.EXCEL)public void testAddFromExcel(@Param(name = "a") Double a, @Param(name = "b") Double b, @Param(name = "expected") Double expected) {Assert.assertEquals(expected, calculator.add(a, b), 0.1);}@Test @DataLoader(filePaths = {"calculator2.xls"})public void testAdd(@Param(name = "a") Double a, @Param(name = "b")Double b, @Param(name = "expected")Double expected){Assert.assertEquals(expected, calculator.add(a,b),0.1);}}和XML數(shù)據(jù)加載器
@RunWith(DataDrivenTestRunner.class) public class XMLLoaderExample extends DataDrivenTest {@Test @DataLoader(filePaths = "calculator2.xml", loaderType = LoaderType.XML)public String testAddXMLOutput(@Param(name = "a") Double a, @Param(name = "b") Double b, @Param(name = "expected") Double expected) {Assert.assertEquals(expected, calculator.add(a, b), 0.1);return "success";} }注意 :
1.文件路徑包含物理路徑,如果文件在類路徑中,則文件名就足夠了。 因此,當(dāng)我定義pom.xml資源時(shí),我們應(yīng)該包括我們的數(shù)據(jù)文件以使其像這樣工作。 或者我們保持正確的輸入路徑。
2.如果使用單個(gè)文件加載,則可以避免使用LoaderType參數(shù)。
3.使用多個(gè)相同類型的文件加載時(shí),請(qǐng)確保沒(méi)有相同的列名。 如果相同,將采用第二個(gè)或更高版本文件的列。 (Lifo方式,將保留最后一個(gè)文件)
4.在同一方法中不支持不同類型的加載器。 因此,您無(wú)法使用單個(gè)數(shù)據(jù)加載器以相同的方法加載Excel&CSV。 只有第一個(gè)會(huì)工作。
5.單個(gè)方法/類不支持多個(gè)數(shù)據(jù)加載器注釋。
6.方法級(jí)數(shù)據(jù)加載器將重載類級(jí)數(shù)據(jù)加載器。
7.將單個(gè)文件用于多個(gè)測(cè)試方法的參數(shù)時(shí),請(qǐng)使用類級(jí)別的數(shù)據(jù)加載器。
8.在參數(shù)中,使用的數(shù)據(jù)以長(zhǎng),字符串,雙精度格式收斂。 但是我們可以將自定義數(shù)據(jù)類型與我們自己的解析器一起使用。 簡(jiǎn)易測(cè)試對(duì)此具有接口。 (使用AbstractConverter <YourDataType>)
9.如果需要自定義顯示的參數(shù)數(shù)據(jù),則可以在加載程序中使用@Display批注。 你可以在這里看到一個(gè)例子 。
這是多個(gè)數(shù)據(jù)加載器的一些示例:
@RunWith(DataDrivenTestRunner.class) public class MultipleDataLoaderExample extends DataDrivenTest {// only the first loader will be working.. // in here "calculator2.csv" @Test @DataLoader(filePaths = {"calculator2.csv","calculator3.xml"})public void testAdd(@Param(name = "a") Double a, @Param(name = "b")Double b, @Param(name = "expected")Double expected) {Assert.assertEquals(expected, calculator.add(a, b), 0.1);} }@RunWith(DataDrivenTestRunner.class) public class MultipleDataLoaderExampleSameType extends DataDrivenTest{@Test @DataLoader(filePaths = {"calculator3.csv","calculator2.csv"})//calculator2 is accepted not 2=> why, it honors the last item, top item of the array list of files public void testAdd(@Param(name = "a") Double a, @Param(name = "b")Double b, @Param(name = "expected")Double expected){Assert.assertEquals(expected, calculator.add(a,b),0.1);}}因此,您可能會(huì)在這里看到更多詳細(xì)的示例。
現(xiàn)在,除了“數(shù)據(jù)加載”之外,“簡(jiǎn)易測(cè)試”還具有許多其他功能。 我沒(méi)有詳細(xì)說(shuō)明,但每個(gè)示例都有示例。 因此,我在逐一解釋的基礎(chǔ)上加了一點(diǎn)。
報(bào)告:簡(jiǎn)單的測(cè)試提供了非常簡(jiǎn)單的報(bào)告……:)。 只需使用注釋即可。 這里有一些例子。
默認(rèn)報(bào)告:(輸入PDF并存儲(chǔ)在用戶目錄中)
@RunWith(DataDrivenTestRunner.class) @Reportpublic class DefaultReportExample extends DataDrivenTest{@Test @DataLoader(filePaths = "calculator2.xls")public void testAdd(@Param(name = "a") Double a, @Param(name = "b") Double b, @Param(name = "expected") Double expected) {Assert.assertEquals(expected, calculator.add(a, b), 0.1);}}
在類路徑中更改類路徑的報(bào)告:(報(bào)告存儲(chǔ)在構(gòu)建目錄中,maven中的目標(biāo)文件夾中)。 將創(chuàng)建一個(gè)文件夾名稱TestReports
@RunWith(DataDrivenTestRunner.class) @Report(outputLocation = "classpath:TestReports") public class ClassPathExampleReport extends DataDrivenTest{@Test @DataLoader(filePaths = "calculator.xls")public void testAddFromExcel(@Param(name = "a") Double a, @Param(name = "b") Double b, @Param(name = "expected") Double expected) {Assert.assertEquals(expected, calculator.add(a, b), 0.1);}@Test @DataLoader(filePaths = "calculator2.xls")public void testAdd(@Param(name = "a") Double a, @Param(name = "b") Double b, @Param(name = "expected") Double expected) {Assert.assertEquals(expected, calculator.add(a, b), 0.1);} }
在文件夾路徑中更改類路徑的報(bào)告:(我們?cè)谖募到y(tǒng)中指定)
@RunWith(DataDrivenTestRunner.class) @Report(outputLocation = "file:TestReports")// default location = project working directorypublic class CustomPathExampleReport extends DataDrivenTest{@Test @DataLoader(filePaths = "calculator.xls")public void testAddFromExcel(@Param(name = "a") Double a, @Param(name = "b") Double b, @Param(name = "expected") Double expected) {Assert.assertEquals(expected, calculator.add(a, b), 0.1);}@Test @DataLoader(filePaths = "calculator2.xls")public void testAdd(@Param(name = "a") Double a, @Param(name = "b") Double b, @Param(name = "expected") Double expected) {Assert.assertEquals(expected, calculator.add(a, b), 0.1);} }
在github倉(cāng)庫(kù)中,我給出了更多易于理解的示例。 這是重要的注意事項(xiàng)。
1.報(bào)告功能測(cè)試報(bào)告和性能測(cè)試報(bào)告有兩種類型(包括運(yùn)行測(cè)試的時(shí)間)。 我們可以創(chuàng)建多個(gè)報(bào)告類型。
2.報(bào)告制作速度慢,因此報(bào)告制作時(shí)間將包含在測(cè)試執(zhí)行時(shí)間中
3.報(bào)告文件格式有3種類型。 Excel,PDF和HTML,其中pdf是默認(rèn)選擇。 我們可以創(chuàng)建多種報(bào)告類型格式。
4. @Report可以在類級(jí)別使用,這意味著在生成時(shí),它包括所有測(cè)試方法結(jié)果。
5.報(bào)表位置可以存儲(chǔ)特定的文件路徑或構(gòu)建目錄中的類路徑。 使用mvn clean時(shí)將清除類路徑報(bào)告,因此請(qǐng)謹(jǐn)慎選擇。
pdf報(bào)告樣本:
并行線程:簡(jiǎn)單測(cè)試具有一個(gè)簡(jiǎn)單的批注@Parallel,我們可以在其中定義JVM將分配多少線程來(lái)測(cè)試一個(gè)測(cè)試類。
@Parallel(threads = 5)//this is very fragilepublic class ParallelTestExample extends DataDrivenTest_withDefaultAnnotation {@Test @DataLoader(filePaths = "calculator.xls", loaderType = LoaderType.EXCEL)public void testAddFromExcel(@Param(name = "a") Double a, @Param(name = "b")Double b, @Param(name = "expected")Double expected){Assert.assertEquals(expected, calculator.add(a,b),0.1);}}您可以看到,此測(cè)試將使用5個(gè)線程運(yùn)行。
我們可以并行運(yùn)行測(cè)試服,也可以并行運(yùn)行
@RunWith(Suite.class) @ParallelSuite(threads = 3) @Suite.SuiteClasses({RepeatExample.class, TestWithPolicyExample.class}) public class ParallelSuitExample { }注意:這非常易碎。 它可能會(huì)導(dǎo)致資源分配錯(cuò)誤。 而簡(jiǎn)單的測(cè)試并不能確保它們是并發(fā)的。
測(cè)試重復(fù):在Easy Test中,我們可以使用注解@Repeat重復(fù)測(cè)試方法。
@RunWith(DataDrivenTestRunner.class) @TestPolicy(PolicyExample.class) public class RepeatExample extends DataDrivenTest {@Test @Repeat(times = 5)public void testAddFromExcel(@Param(name = "a") Double a,@Param(name = "b") Double b,@Param(name = "expected") Double expected) {Assert.assertEquals(expected, calculator.add(a, b), 0.1);} }它是串行運(yùn)行的,而不是并行運(yùn)行的。
測(cè)試屬性:簡(jiǎn)易測(cè)試具有一個(gè)不錯(cuò)的注解@TestProperties,可用于直接從類路徑中注入測(cè)試屬性。
public class TestPropertiesExample extends DataDrivenTest_withDefaultAnnotation {@TestProperties(value = {"test.properties"})private Properties myProps;@Test public void testAddition() {Assert.assertEquals("Easy Test Demos", myProps.getProperty("test.title"));} }[忽略延伸部分,不需要。
為了保持良好的測(cè)試,我用過(guò)。
您可以看到表格github來(lái)源。
]
測(cè)試策略:測(cè)試策略在Easy測(cè)試中非常有用,我們可以在單獨(dú)的類中定義測(cè)試策略并在測(cè)試類中使用它。
為此,定義一個(gè)策略類。
@Ignore@Parallel(threads = 2) @Report(reportTypes = {Report.REPORT_TYPE.DEFAULT,Report.REPORT_TYPE.METHOD_DURATION},outputFormats = Report.EXPORT_FORMAT.PDF,outputLocation = "file:TestReports") @DataLoader(filePaths = "calculator.xls")// i preffer data loder should be with method@Display(fields = "id") public class PolicyExample { }并在測(cè)試課程中使用它
@RunWith(DataDrivenTestRunner.class) @TestPolicy(PolicyExample.class) public class TestWithPolicyExample extends DataDrivenTest {@Test public void testAddFromExcel(@Param(name = "a") Double a, @Param(name = "b") Double b, @Param(name = "expected") Double expected) {Assert.assertEquals(expected, calculator.add(a, b), 0.1);} } 因此,我們可以看到,該策略將為線程定義數(shù)據(jù)文件+報(bào)告和并行線程。
在哪里使用?
=>在測(cè)試期間,如果我們認(rèn)為需要單獨(dú)進(jìn)行測(cè)試配置。
=>當(dāng)我們具有單獨(dú)的測(cè)試配置以在不同情況下運(yùn)行時(shí)。 例如在開(kāi)發(fā)人員PC或CI中進(jìn)行測(cè)試,或用于測(cè)試報(bào)告,或進(jìn)行不同類型的測(cè)試等。
缺點(diǎn):
1.錯(cuò)誤消息不是用戶友好的。 因此調(diào)試并不容易。 您需要了解簡(jiǎn)單的測(cè)試架構(gòu)才能理解錯(cuò)誤。
2.并行執(zhí)行可能導(dǎo)致與錯(cuò)誤相關(guān)的資源被鎖定或繁忙。
3. Excel或CSV中的空白字符可能會(huì)導(dǎo)致錯(cuò)誤。 因此,在創(chuàng)建數(shù)據(jù)文件時(shí)要小心。
4.存在一些已知的錯(cuò)誤。 流行的錯(cuò)誤之一是,僅通過(guò)excel數(shù)據(jù)加載器生成的測(cè)試失敗報(bào)告。 其他數(shù)據(jù)加載器無(wú)法生成失敗測(cè)試報(bào)告(僅生成通過(guò)測(cè)試報(bào)告)。
我希望這篇文章將有助于使用Easy test。 我的Github示例:https://github.com/sarkershantonu/Automation-Getting-Started/tree/master/junit-easytest
Todo:自定義數(shù)據(jù)類型轉(zhuǎn)換器示例。 我將逐漸添加。
翻譯自: https://www.javacodegeeks.com/2016/12/data-driven-testing-junit-easytest.html
總結(jié)
以上是生活随笔為你收集整理的使用Junit和Easytest进行数据驱动的测试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 福禄寿喜财什么意思 福禄寿喜财代表什么
- 下一篇: mapreduce 聚合_MapRedu