지난 글에 이어서 http://www.mkyong.com/tutorials/testng-tutorials/ 에 잘 정리된 TestNG 튜토리얼을 제 블로그에 다시 정리 합니다.
TestNG Tutorial 6 – Parameterized Test
The “Parameterized Test” means vary parameter value for unit test. In TestNG, XML file or “@DataProvider” is used to provide vary parameter for unit testing.
TestNG에서는 XML 파일이나 @DataProvider 를 사용해서 파라미터를 전달하네요.
지난번 Selenium에 대한 글에서 이 부분도 있었던 것 같습니다.
jUnit 에서 Mockito를 사용해서 객체를 mocking하고 inject 를 사용해서 데이터를 세팅하는 것과 비슷한 일을 한다는 인상을 받았는데 이 예제를 보고 확실하게 그런지 어떤지 공부해 봐야겠습니다.
1. XML file for parameterized test.
Declare “@Parameters” annotation in method which needs parameter testing, the parametric data will be provide by TestNG’s XML configuration files. By doing this, you can reuse a single test case with different data sets easily. In addition, even end user, QA or QE can provide their own data sets for testing.
import org.testng.annotations.*; /** * TestNG Parameterized Test * @author mkyong * */ public class TestNGTest6_1_0 { @Test @Parameters(value="number") public void parameterIntTest(int number) { System.out.println("Parameterized Number is : " + number); } }
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" > <suite name="My test suite"> <test name="testing"> <parameter name="number" value="2"/> <classes> <class name="TestNGTest6_1_0" /> </classes> </test> </suite>
Result
Parameterized Number is : 2
jUnit 에서 봤던 mocking and injecting 이랑은 좀 다르군요.
TestNG에서는 해당 테스트 메소드에 직접 데이터를 파라미터로 전달하는 방법이 있네요.
jUnit을 사용할 때 이런 방법을 사용한적은 없는것 같은데....
TestNG에만 있는 기능인지 아니면 제가 field에서 jUnit의 이런 기능을 사용하지 않았던가 하겠네요.
2. @DataProvider for parameterized test.
While pulling data values into an XML file can be quite handy, but test cases occasionally may require complex data types, which can’t be represented as a String or a primitive value in XML file. TestNG handles this scenario with @DataProvider annotation, which facilitates the mapping of complex parameter types to a test method.
1. @DataProvider for Vector, String or Integer as parameter
import java.util.Vector; import org.testng.annotations.*; /** * TestNG Parameterized Test - Advance * @author mkyong * */ public class TestNGTest6_2 { @Test(dataProvider = "Data-Provider-Function") public void parameterIntTest(Class clzz, String[] number) { System.out.println("Parameterized Number is : " + number[0]); System.out.println("Parameterized Number is : " + number[1]); } //This function will provide the parameter data @DataProvider(name = "Data-Provider-Function") public Object[][] parameterIntTestProvider() { return new Object[][]{ {Vector.class, new String[] {"java.util.AbstractList", "java.util.AbstractCollection"}}, {String.class, new String[] {"1", "2"}}, {Integer.class, new String[] {"1", "2"}} }; } }
Result
Parameterized Number is : java.util.AbstractList Parameterized Number is : java.util.AbstractCollection Parameterized Number is : 1 Parameterized Number is : 2 Parameterized Number is : 1 Parameterized Number is : 2 PASSED: parameterIntTest(class java.util.Vector, [Ljava.lang.String;@1016632) PASSED: parameterIntTest(class java.lang.String, [Ljava.lang.String;@10a6ae2) PASSED: parameterIntTest(class java.lang.Integer, [Ljava.lang.String;@4a6cbf)
2. @DataProvider for object as parameter
“TestNGTest6_3_0” is a simple object with simple get set methods.
/** * TestNG Parameterized Test - Advance * @author mkyong * */ public class TestNGTest6_3_0 { private int number; private String msg; public void setNumber(int number){ this.number = number; } public int getNumber(){ return this.number; } public void setMsg(String msg){ this.msg = msg; } public String getMsg(){ return this.msg; } }
import org.testng.annotations.*; /** * TestNG Parameterized Test - Advance * @author mkyong * */ public class TestNGTest6_3_1 { @Test(dataProvider = "Data-Provider-Function") public void parameterIntTest(TestNGTest6_3_0 clzz) { System.out.println("Parameterized Number is : " + clzz.getMsg()); System.out.println("Parameterized Number is : " + clzz.getNumber()); } //This function will provide the patameter data @DataProvider(name = "Data-Provider-Function") public Object[][] parameterIntTestProvider() { TestNGTest6_3_0 obj = new TestNGTest6_3_0(); obj.setMsg("Hello"); obj.setNumber(123); return new Object[][]{ {obj} }; } }
Result
Parameterized Number is : 123 PASSED: parameterIntTest(TestNGTest6_3_0@dc6a77)
TestNG’s Parameterized test is very user friendly and flexible (either in XML file or inside the class). It can support many complex data type as parameter value and the possibility is unlimited. As example above, you even can pass in your own object (TestNGTest6_3_0) for Parameterized test.
이걸 이용하면 테스트 케이스를 작성해 놓고 유저가 원하는 데이터를 보내서 테스트를 해 볼 수 있겠습니다.
그런데 이 기능은 FitNesse 를 사용하면 훨씬 더 좋을 것 같군요.
TestNG parameter testing example
TestNG parameter testing example.
Problem
Let’s say, a utility class has a function for converting the character to ASCII or vice verse, how can you test it with TestNG?
Solution
You can create a unit test function which accept two parameters (character and expected ASCII) from TestNG data provider, and assert the value like following :
Example in Java
package com.mkyong.common; /** * Character Utility class * @author mkyong * */ public class CharUtils { /** * Convert the characters to ASCII value * @param character character * @return ASCII value */ public static int CharToASCII(final char character){ return (int)character; } /** * Convert the ASCII value to character * @param ascii ascii value * @return character value */ public static char ASCIIToChar(final int ascii){ return (char)ascii; } }
Unit Test
package com.mkyong.common; import org.testng.Assert; import org.testng.annotations.*; /** * Character Utils Testing * @author mkyong * */ public class CharUtilsTest { @DataProvider public Object[][] ValidDataProvider() { return new Object[][]{ { 'A', 65 },{ 'a', 97 }, { 'B', 66 },{ 'b', 98 }, { 'C', 67 },{ 'c', 99 }, { 'D', 68 },{ 'd', 100 }, { 'Z', 90 },{ 'z', 122 }, { '1', 49 },{ '9', 57 }, }; } @Test(dataProvider = "ValidDataProvider") public void CharToASCIITest(final char character, final int ascii) { int result = CharUtils.CharToASCII(character); Assert.assertEquals(result, ascii); } @Test(dataProvider = "ValidDataProvider") public void ASCIIToCharTest(final char character, final int ascii) { char result = CharUtils.ASCIIToChar(ascii); Assert.assertEquals(result, character); } }
Result
PASSED: CharToASCIITest(A, 65) PASSED: CharToASCIITest(a, 97) PASSED: CharToASCIITest(B, 66) PASSED: CharToASCIITest(b, 98) PASSED: CharToASCIITest(C, 67) PASSED: CharToASCIITest(c, 99) PASSED: CharToASCIITest(D, 68) PASSED: CharToASCIITest(d, 100) PASSED: CharToASCIITest(Z, 90) PASSED: CharToASCIITest(z, 122) PASSED: CharToASCIITest(1, 49) PASSED: CharToASCIITest(9, 57) PASSED: ASCIIToCharTest(A, 65) PASSED: ASCIIToCharTest(a, 97) PASSED: ASCIIToCharTest(B, 66) PASSED: ASCIIToCharTest(b, 98) PASSED: ASCIIToCharTest(C, 67) PASSED: ASCIIToCharTest(c, 99) PASSED: ASCIIToCharTest(D, 68) PASSED: ASCIIToCharTest(d, 100) PASSED: ASCIIToCharTest(Z, 90) PASSED: ASCIIToCharTest(z, 122) PASSED: ASCIIToCharTest(1, 49) PASSED: ASCIIToCharTest(9, 57) =============================================== com.mkyong.common.CharUtilsTest Tests run: 24, Failures: 0, Skips: 0 =============================================== =============================================== mkyong Total tests run: 24, Failures: 0, Skips: 0 ===============================================
TestNG Tutorial 7 – Dependency Test
The “Dependency Test” means methods are test base on dependency. If the dependent method fails, all the subsequent test methods will be skipped, not marked as failed.
TestNG uses “dependOnMethods“ to implement the dependency testing as following
이 기능은 어떤 특정 테스트가 실행되고 난 다음에 다른 메소드를 실행하고 싶을 때 사용되는 기능입니다.
import org.testng.annotations.*; /** * TestNG Dependency Test * @author mkyong * */ public class TestNGTest7 { @Test public void method1() { System.out.println("This is method 1"); } @Test(dependsOnMethods={"method1"}) public void method2() { System.out.println("This is method 2"); } }
Result
PASSED: method1 PASSED: method2
The “method2()” will execute only if “method1()” is run successfully, else “method2()” will skip.
튜토리얼을 쭉 봤는데요.
데이터를 Mocking 해야 될 때 TestNG는 어떻게 하는지 잘 모르겠네요.
jUnit 에서는 Mockito를 사용했는데 TestNG에서도 그런지....
다음에는 이 방법을 좀 알아봐야 겠습니다.
'TDD Project' 카테고리의 다른 글
JAVA DATE 함수 사용. 날짜 계산하기 (0) | 2014.01.27 |
---|---|
미국 IT 프로젝트 참여 과정과 개발환경 세팅하기 (0) | 2013.11.15 |
Jenkins 에 대한 개요 (0) | 2013.11.08 |
새로운 프로젝트, 새로운 기술, 배울것들이 많아서 좋다. (1) | 2013.11.07 |
JUnit 4 와 TestNG 비교하기 (0) | 2013.11.03 |
TestNG Tutorial 정리 01 (0) | 2013.11.01 |
java memo 두 날짜 사이 일 수 구하기 (0) | 2013.09.05 |
Hannah Anderson Dismisses Critics, Explains Relationship with James DiMaggio (0) | 2013.08.22 |
ConstraintFixture,SetFixture,SubsetFixture Tutorial - FitLibrary - (0) | 2013.08.22 |
CombinationFixture Tutorial - FitLibrary - (0) | 2013.08.22 |