junit数据驱动测试_使用Junit和Easytest进行数据驱动的测试
junit數據驅動測試
在本文中,我們將看到如何使用Junit進行數據驅動的測試。 為此,我將使用一個名為EasyTest的庫。
我們知道,對于TestNG,它已內置了數據提供程序。 通過簡單的測試,我們可以使用Junit進行數據驅動的測試。
什么是數據驅動測試?
當測試由數據驅動時,則指的是數據驅動的測試。 正式定義可以在Wiki中找到。
總之,將對您的輸入數據,預期輸出,配置等進行參數化定義。 因此,最后,您不需要更改測試,但是隨著數據的更改,您可以增加測試數量和覆蓋范圍。 這意味著,您的數據將驅動您的測試能力以及質量。
當您需要處理具有不同排列和組合的大量數據時,這是一種非常好的方法。
我在哪里使用這種方法?
1.當我需要檢查大量數據并將其針對數據庫查詢或Web api方法(REST / SOAP)輸出時。
2.當我需要使用不同的數據組合來驅動相同的UI測試時。
3.當我需要將日期更改行為與配置更改隔離開來時。
我們將如何實現?
我們將通過參數化測試解決此問題。 并且此參數將從我們定義的文件中獲取值(測試數據)。 這是TestNG與Junit一起缺少的部分。 我們將使用EasyTest庫解決。
注意:這不是我們使用Easy test的唯一原因。 簡易測試還具有許多令人印象深刻的功能。 我們將一一看到。 這是github中的簡單測試項目鏈接。
讓我們學習Example:
為了學習,我正在使用簡單的計算器類( Github鏈接 )。 只需將兩個數字相加即可得到Double類型的結果。
public class Calculator {public Double add(Double a, Double b){return a+b;} }并且,讓我們在沒有Easy Test的情況下創建一個測試用例。
public class SimpleTest extends DataDrivenTest{@Test public void testAddition(){Assert.assertEquals(25.5,calculator.add(10.5,15.0),0.01);} }我們將使用Easy Test來開發這個簡單的測試用例,因此讓我們開始制作一個項目。
步驟A:創建Maven項目:
1.使用您最喜歡的組ID和工件ID創建一個Maven項目。 (我使用org.automation和數據驅動)
2.包括以下依賴項。
對于Junit(我們正在使用)
dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version> </dependency>輕松測試
<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>[這足以使用簡單的測試內部日志記錄。
]
現在,要提供數據參數,我們需要包括數據文件。 作為最佳實踐,我會將所有數據都用作資源 。 因此,我需要在pom中包含資源。 因此,最終項目pom.xml看起來像這樣 。
<?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:創建數據文件:簡易測試支持
1. Excel:Office 97/2003格式(xls擴展名)
2. CSV:逗號分隔值
3. XML
4.通過CSV文件的JSON格式。
注意:Easy test還支持自定義數據類型(我們將跳過此部分以使其在Blog中變得簡單,您可以在Github中查看示例)
對于CSV和EXCEL,請遵循以下規則
1.第一行,第一列為方法名稱(因此,此列的每一行將為空白)
2.第一行從第二列開始,所有將均為參數變量名稱。
3.寫入方法名稱的列的所有行均為空白。
CSV:
Excel(.xls):
XML:
您可以在github中看到我所有的文件。
現在,讓我們從其他數據加載器加載數據。
要加載數據,通常我們使用注釋
1. @DataLoader定義文件源。
2. @Param定義將哪些列數據視為要獲取的項目。
由于此批注特定于Easy Test,因此我們需要使用DataDrivenTestRunner.class來運行此測試類,該驅動程序由
因此,首先我們看到CSV數據加載器。
@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運行測試
=>正在加載calculator.csv(還有另外一個)
=>獲取名稱為a,b的參數。
這是CSV文件的內容,看起來像
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現在,您可能會問如何處理行。 輕松測試將每一行視為一條記錄,它將根據數據文件中存在的行數來迭代我們的測試。 因此,為輸入數據定義列已綽綽有余。
最佳實踐:我過去一直遵循一個文件來提供輸入數據以及預期的輸出。 您可以為此使用單獨的文件。
同樣,如果我們查看excel數據加載器:
@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數據加載器
@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.文件路徑包含物理路徑,如果文件在類路徑中,則文件名就足夠了。 因此,當我定義pom.xml資源時,我們應該包括我們的數據文件以使其像這樣工作。 或者我們保持正確的輸入路徑。
2.如果使用單個文件加載,則可以避免使用LoaderType參數。
3.使用多個相同類型的文件加載時,請確保沒有相同的列名。 如果相同,則將保留第二個或更高版本文件的列。 (Lifo方式,將保留最后一個文件)
4.在同一方法中不支持不同類型的加載器。 因此,您無法使用單個數據加載器以相同的方法加載Excel&CSV。 只有第一個會工作。
5.單個方法/類不支持多個數據加載器注釋。
6.方法級數據加載器將重載類級數據加載器。
7.將單個文件用于多個測試方法的參數時,請使用類級別的數據加載器。
8.在參數中,使用的數據以長,字符串,雙精度格式收斂。 但是我們可以將自定義數據類型與我們自己的解析器一起使用。 簡易測試對此具有接口。 (使用AbstractConverter <YourDataType>)
9.如果需要自定義顯示的參數數據,則在加載程序時可以使用@Display批注。 你可以在這里看到一個例子 。
以下是多個數據加載器的一些示例:
@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);}}因此,您可能會在這里看到更多詳細的示例。
現在,除了“數據加載”之外,“簡易測試”還具有許多其他功能。 我沒有詳細說明,但每個示例都有示例。 因此,我在逐一解釋的基礎上加了一點。
報告:簡單的測試提供了非常簡單的報告……:)。 只需使用注釋即可。 這里有一些例子。
默認報告:(輸入PDF并存儲在用戶目錄中)
@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);}}
在類路徑中更改類路徑的報告:(報告存儲在構建目錄中,maven中的目標文件夾中)。 將創建一個文件夾名稱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);} }
在文件夾路徑中更改類路徑的報告:(我們在文件系統中指定)
@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倉庫中,我給出了更多易于理解的示例。 以下是重要注意事項。
1.報告功能測試報告和性能測試報告有兩種類型(包括運行測試的時間)。 我們可以創建多個報告類型。
2.報告制作速度慢,因此報告制作時間將包含在測試執行時間中
3.報告文件格式有3種類型。 Excel,PDF和HTML,其中pdf是默認選擇。 我們可以創建多種報告類型格式。
4. @Report可以在類級別使用,這意味著在生成時,它包括所有測試方法結果。
5.報表位置可以存儲特定的文件路徑或構建目錄中的類路徑。 當我們使用mvn clean時,將清除類路徑報告,因此請謹慎選擇。
pdf報告樣本:
并行線程:簡便測試具有一個簡單的@Parallel批注,在其中我們可以定義JVM將分配多少線程來測試一個測試類。
@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);}}您可以看到,此測試將使用5個線程運行。
我們可以并行運行測試服,也可以并行運行
@RunWith(Suite.class) @ParallelSuite(threads = 3) @Suite.SuiteClasses({RepeatExample.class, TestWithPolicyExample.class}) public class ParallelSuitExample { }注意:這非常易碎。 它可能會導致資源分配錯誤。 而簡單的測試并不能確保這些并發。
測試重復:在Easy Test中,我們可以使用注解@Repeat重復測試方法。
@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);} }它是串行運行的,而不是并行運行的。
測試屬性:簡易測試具有一個不錯的注解@TestProperties,可用于直接從類路徑中注入測試屬性。
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"));} }[忽略延伸部分,不需要。
為了保持良好的測試,我用過。
您可以看到表格github來源。
]
測試策略:測試策略在Easy測試中非常有用,我們可以在單獨的類中定義測試策略并在測試類中使用它。
為此,定義一個策略類。
@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 { }并在測試課程中使用
@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);} } 因此,我們可以看到,該策略將為線程定義數據文件+報告和并行線程。
在哪里使用?
=>在測試過程中,如果我們認為需要單獨進行測試配置。
=>當我們具有單獨的測試配置以在不同情況下運行時。 例如在開發人員PC或CI中進行測試,或用于測試報告,或進行不同類型的測試等。
缺點:
1.錯誤消息不是用戶友好的。 因此調試并不容易。 您需要了解簡單的測試架構才能理解錯誤。
2.并行執行可能導致與錯誤相關的資源被鎖定或繁忙。
3. Excel或CSV中的空白字符可能會導致錯誤。 因此,在創建數據文件時要小心。
4.存在一些已知的錯誤。 流行的錯誤之一是,僅通過excel數據加載器生成的測試失敗報告。 其他數據加載器無法生成失敗測試報告(僅生成通過測試報告)。
希望本文有助于使用Easy test。 我的Github示例:https://github.com/sarkershantonu/Automation-Getting-Started/tree/master/junit-easytest
Todo:自定義數據類型轉換器示例。 我將逐漸添加。
翻譯自: https://www.javacodegeeks.com/2016/12/data-driven-testing-junit-easytest.html
junit數據驅動測試
總結
以上是生活随笔為你收集整理的junit数据驱动测试_使用Junit和Easytest进行数据驱动的测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怡口软水机设置(怡口软水机设置反冲洗)
- 下一篇: 本地缓存防止缓存击穿_防止缓存爆炸的快速