junit testng
继续进行同样的战斗,昨天我开始研究Spring的测试支持 。 它还建立在JUnit之上。 但是,在使用相同功能的几分钟后,我一直在寻找JUnit中一直找不到的功能。 TestNG使用DataProviders提供参数化测试 。 考虑到我再次问自己一个熟悉的问题– TestNG或JUnit,我决定对此进行记录,以便下次可以确定是哪个以及为什么。
基本上一样
如果您只是要进行一些基本的单元测试,那么这两个框架基本上是相同的。 这两个框架都允许您快速有效地测试代码。 他们在Eclipse和其他IDE中具有工具支持。 他们还在Ant和Maven等构建框架中获得了支持。 对于初学者来说,JUnit一直是首选,因为它是第一个用于单元测试的框架,并且一直可用。 我谈论的许多人直到我们谈论它之前都没有听说过TestNG。
灵活性
让我们来看两个简单的测试用例。
package com.kapil.itrader;
import java.util.Arrays;
import java.util.List;
import junit.framework.Assert;
import org.junit.BeforeClass;
import org.junit.Test;public class FibonacciTest
{private Integer input;private Integer expected;@BeforeClasspublic static void beforeClass(){// do some initialization}@Testpublic void FibonacciTest(){System.out.println("Input: " + input + ". Expected: " + expected);Assert.assertEquals(expected, Fibonacci.compute(input));assertEquals(expected, Fibonacci.compute(input));}
}
好吧,这是示例展示,我正在使用4.x +版本,并且正在使用注释。 优先于4.0版本; JUnit不支持注释,这是TestNG相对于竞争对手的主要优势; 但是JUnit很快就适应了。 您可能会注意到,JUnit还支持静态导入,并且像以前的版本一样,我们可以省去更多麻烦的代码。
package com.kapil.framework.core;
import junit.framework.Assert;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;public class BaseTestCase
{protected static final ClassPathXmlApplicationContext context;static{context = new ClassPathXmlApplicationContext("rootTestContext.xml");context.registerShutdownHook();}@BeforeSuiteprivate void beforeSetup(){// Do initialization}@Testpublic void testTrue(){Assert.assertTrue(false);}
}
首先看一下这两个代码,可以推断两者几乎相同。 但是,对于那些已经完成足够的单元测试的人,我会同意TestNG可以提供更大的灵活性。 JUnit要求我将初始化方法声明为static; 因此,我将在该方法中编写的任何内容也必须是静态的。 JUnit还要求我将初始化方法公开。 但是TestNG不会。 我也可以在测试课程中使用OOP的最佳做法。 除了@ BeforeClass,TestNG还允许我声明测试套件,组,方法,并使用@ BeforeSuite,@ BeforeMethod,@ BeforeGroups之类的批注。 当编写需要访问公共数据集的任何级别的集成测试或单元测试用例时,这非常有用。
测试隔离和依赖性测试
在进行隔离测试时,Junit非常有效。 从本质上讲,这意味着您无法控制测试的执行顺序。 并且,因此,如果由于某种依赖关系而有两个要按特定顺序运行的测试,则无法使用JUnit进行。 但是,TestNG允许您非常有效地执行此操作。 在Junit中,您可以解决此问题,但它并不整齐,也不那么容易。
基于参数的测试
TestNG提供的一个非常强大的功能是“参数化测试”。 JUnit在4.5+版本中为此添加了一些支持,但是它不如TestNG有效。 您可能与FIT合作过,您会知道我在说什么。 但是,JUnit中添加的支持非常基础,并且效果不佳。 我已经修改了我以前的测试用例,以包括参数化测试。
package com.kapil.itrader;import static org.junit.Assert.assertEquals;import java.util.Arrays;
import java.util.List;import junit.framework.Assert;import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;@RunWith(Parameterized.class)
public class FibonacciTest
{private Integer input;private Integer expected;@Parameterspublic static List data(){return Arrays.asList(new Integer[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });}@BeforeClasspublic static void beforeClass(){System.out.println("Before");}public FibonacciTest(Integer input, Integer expected){this.input = input;this.expected = expected;}@Testpublic void FibonacciTest(){System.out.println("Input: " + input + ". Expected: " + expected);Assert.assertEquals(expected, Fibonacci.compute(input));assertEquals(expected, Fibonacci.compute(input));}}
您会注意到,我已经使用@RunWith批注来对测试用例进行参数化。 在这种情况下,将使用已用@Parameters注释的内联方法– data()将数据提供给该类。 但是,最大的问题是将数据传递给类构造函数。 这使我只能在此类中编写逻辑绑定的测试用例。 而且,我最终将对一个服务拥有多个测试用例,因为该服务中的所有各种方法都需要不同的数据集。 好消息是,有各种开放源代码框架扩展了此方法,并添加了自己的“ RunWith”实现,以允许与CSV,HTML或Excel文件等外部实体集成。
TestNG提供了开箱即用的支持。 不支持读取CSV或外部文件,但支持数据提供程序。
package com.kapil.itrader.core.managers.admin;import org.testng.Assert;
import org.testng.annotations.Test;import com.uhc.simple.common.BaseTestCase;
import com.uhc.simple.core.admin.manager.ILookupManager;
import com.uhc.simple.core.admin.service.ILookupService;
import com.uhc.simple.dataprovider.admin.LookupValueDataProvider;
import com.uhc.simple.dto.admin.LookupValueRequest;
import com.uhc.simple.dto.admin.LookupValueResponse;/*** Test cases to test {@link ILookupService}.*/
public class LookupServiceTests extends BaseTestCase
{@Test(dataProvider = "LookupValueProvider", dataProviderClass = LookupValueDataProvider.class)public void testGetAllLookupValues(String row, LookupValueRequest request, LookupValueResponse expectedResponse){ILookupManager manager = super.getLookupManager();LookupValueResponse actualResponse = manager.getLookupValues(request);Assert.assertEquals(actualResponse.getStatus(), expectedResponse.getStatus());}
}
上面的代码片段展示了我已经将dataProvider用作批注的值,然后提供了一个类,该类负责创建在调用时提供给方法的数据。 使用这种机制,我可以轻松地以分离的方式编写测试用例及其数据提供程序,并非常有效地使用它。
为什么我选择TestNG
对我来说,参数化测试是我选择TestNG而不是Junit的最大原因。 但是,上面列出的所有内容都是为什么我总是要花几分钟的时间在新的Eclipse安装程序或maven项目中设置TestNG的原因。 当运行大型测试套件时,TestNG非常有用。 对于小型项目或培训活动,JUnit很好; 因为任何人都可以很快开始使用它; 但不适用于我们需要数千个测试用例的项目,并且在大多数测试用例中,您将涵盖各种方案。
参考: Scratch Pad博客上来自我们的JCG合作伙伴 Kapil Viren Ahuja的TestNG或JUnit 。
翻译自: https://www.javacodegeeks.com/2012/01/testng-or-junit.html
junit testng