您好,欢迎访问三七文档
当前位置:首页 > 中学教育 > 初中教育 > testng+mockito+unitils培训
单元测试基础知识培训主讲人:陈雄华内容提纲•单元测试基本概念•testng简介•unitils简介•mockito简介•单元测试实践前言本次培训主要试图解决以下的问题:1.使用System.out.print肉眼观察-效率低下,容易出错2.测试环境往往手工构造-搭建困难,效率低下3.测试环境易产生垃圾,引发脆弱的测试-不可重复测试4.测试环境依赖性大-过多外部依赖5.测试过于简单-大量BUG转送给系统测试人员本次培训主要试图解决以下的问题:1.使用System.out.print肉眼观察-效率低下,容易出错2.测试环境往往手工构造-搭建困难,效率低下3.测试环境易产生垃圾,引发脆弱的测试-不可重复测试4.测试环境依赖性大-过多外部依赖5.测试过于简单-大量BUG转送给系统测试人员单元测试基本概念为什么要做单元测试(1)publicstaticintbuggyBinarySearch(ListIntegerlist,inttarget){intlow=0;inthigh=list.size()-1;while(low=high){intmid=(low+high)/2;intmidVal=list.get(mid);if(midValtarget)low=mid+1;elseif(midValtarget)high=mid-1;elsereturnmid;}return-1;}为什么要做单元测试(2)软件质量的最简单,最有效的保证最简单,最有效的文档回归测试重构的基石持续集成的基石基本概念(1)SUT–SystemUnderTest被测系统实际上是指“要测试的东西”,如类或方法。基本概念(2)TestDouble主要是为了测试进行下去。基本概念(3)TestFixture测试夹具是一个比较难定义的概念。《xUnitTestPatterns》有这样的一句话“ThetestfixtureiseverythingweneedtohaveinplacetoexercisetheSUT.”作者使用了everything这个词,说明TestFixture的确是一个很难定义的概念。简单的说,执行一个或一组单元测试的时候的一些独立的或共同的测试环境。TestFixture(1)TestFixture(2)基本概念(4)TestCase单元测试的最小单元。TestSuite测试套件,在testng中还有分组的概念,它是对测试套件更细力度的划分。Assertions断言,来验证程序的正确性。TestNG简介为什么是TestNG上面的图比较简单的直观的反映了TestNG在单元测试框架方面的优势。除了有上面的优势外,TestNG还有一些其它的优势,如1,支持更细化的生命周期。2,支持并行计算。3,对持续集成有些更好的支持等。生命周期(1)生命周期(2)Basic(1)@Test标注一个测试方法。@TestpublicvoidtestAdd(){//...}Basic(2)@Test(enable=false)忽略它所标注的测试方法,一般我们可以使用它来标识一些暂时不能满足测试条件的测试方法来避免测试失败,类似JUnit4的@Ignore。@Test(enabled=false)publicvoidtestIgnore(){System.out.println(Methodisnotreadyyet);}Basic(3)@Test(timeout=1000)跟JUnit4一样,TestNG也支持超时测试。@Test(timeOut=1000,skipFailedInvocations=true)publicvoidtestSpendLotOfTime(){Thread.sleep(2000);}Basic(4-1)异常测试方式一@TestpublicvoidtestDivisionWithException(){try{inti=1/0;fail(Thetestshouldhavefailed);}catch(ArithmeticExceptionex){//success,donothing,justignored.}}Basic(4-2)异常测试方式二@Test(expectedExceptions=ArithmeticException.class)publicvoidtestDivisionWithException(){inti=1/0;}Basic(5)依赖测试@Test(dependsOnMethods=…)@Test(dependsOnGroups=…)Demo参数化测试(1)DataProvider为测试方法提供测试数据,它也可以看成是一个特殊的测试夹具。参数化测试(2)@Test(dataProvider=bean-parameter)publicvoidtestParameter(TestBeanclazz){Assert.assertEquals(clazz.getMsg(),Hello);Assert.assertEquals(clazz.getNumber(),5);}参数化测试(3)@DataProvider(name=bean-parameter)publicObject[][]beanParameterProvider(){TestBeanobj=newTestBean();obj.setMsg(Hello);obj.setNumber(5);returnnewObject[][]{{obj}};}参数化测试(4)@Parameters(value=…)这个参数的值需要在XML文件里设置。类似于JUnit4中@Parameters参数化测试(5)@Test@Parameters(value=number)publicvoidtestParameter(intnumber){assertEquals(number,5);}分组@Test(groups=…)TestNG提供了分组的概念,能更加灵活的测试类分类。@Test(groups=fast-tests)publicvoidfastMethod1(){System.out.println(FastMethods#1);}Unitils简介Unitils是什么?Isanopensourcetestinglibrary.Makeunittestingeasyandmaintainable.Providesgeneralassertionutilites.Supportfordatabasetesting.Supportfortestingwithmockobjects.Offerintegrationwithotherframeworks.Morescalable.为什么要用Unitils提供良好测试环境提供良好持久层测试方法(如,对hibernate的mapping的测试)易于做自动测试易于与其它的框架整合...Unitils简介unitils主要是基于Module的方式来开发的,可以在unitils.properties文件中配置各模块的属性。与测试框架集成与其它框架集成DBUnit@DataSet提供测试数据@ExpectedDataSet提供验证数据Hibernate@HibernateSessionFactory与hibernate集成Spring@SpringApplictoinContext启动Spring容器@SpringBean绑定Bean@SpringBeanByName绑定Bean@SpringBeanByType绑定Bean持久层测试维护测试数据库提供测试数据数据的独立性MappingtestpublicclassMappingTestextendsUnitilsJUnit4{//….@HibernateSessionFactory({hibernate.cfg.xml})privateSessionFactorysessionFactory;@TestpublicvoidtestMappingToDatabase(){HibernateUnitils.assertMappingWithDatabaseConsistent();}//…}加载测试数据(1)@DataSet●运行测试之前清空旧有数据●运行测试之前加载测试数据●默认文件:相同的包名+测试类名.xmlBlogDaoTest.xml?xmlversion='1.0'encoding='UTF-8'?dataset[…XSDdeclaration…]blogid=1createAt=2010-12-18ownerName=tom//dataset加载测试数据(2)DaoExamplepublicclassBlogDaoImplextendsBaseDaoimplementsIBlogDao{//…publicBloggetBlog(IntegerblogId){return(Blog)getHibernateTemplate().get(Blog.class,blogId);}//…}DaoExampleTest@DataSetpublicclassBlogDaoTestextendsUnitilsJUnit4{@HibernateSessionFactory({hibernate.cfg.xml})privateSessionFactorysessionFactory;privateIBlogDaoblogDao;@BeforeMethodpublicvoidsetUp(){blogDao=newBlogDaoImpl();((BlogDaoImpl)blogDao).setSessionFactory(sessionFactory);}@TestpublicvoidgetBlogTest(){BlogtestResult=blogDao.getBlog(1);//GetthefirstblogassertNotNull(blog表应该存在一条主键为1的记录,testResult);//…}}加载测试数据(3)自定义文件名@DataSet(“SimpleBlogDaoTest.xml”)publicclassBlogDaoTestextendsUnitilsJUnit4{单个测试方法指定数据集@DataSetpublicvoidgetBlogTest(){相当于getBlogTest.xml(契约模式)加载多个数据集@DataSet(“SimpleBlogDaoTest1.xml”,“SimpleBlogDaoTest2.xml”)publicclassBlogDaoTestextendsUnitilsJUnit4{加载测试数据(4)数据集XML文件格式验证Unitils根据表结构自动生成XSD或DTD格式验证?xmlversion='1.0'encoding='UTF-8'?datasetxmlns:xsi=:noNamespaceSchemaLocation=dataset.xsdblogid=1createAt=2010-12-18ownerName=tom//dataset加载测试数据(5)多个数据库schema的支持?xmlversion='1.0'encoding='UTF-8'?datasetxmlns:schema1=“SCHEMA1”xmlns:schema2=SCHEMA2blogid=1createAt=2010-12-18ownerName=tom//dataset验证数据库内容@ExpectedDataSet测试方法执行完后验证数据库内容验证数据库内容-Example@Test@DataSet(BlogDaoTest.Sav
本文标题:testng+mockito+unitils培训
链接地址:https://www.777doc.com/doc-5480516 .html