1.zk代码怎么做unit test
往简单了说,在没有返回值的方法前加上@Test ,然后将junit的jar包引入就好了。
这个ide都会有的,直接引入就行了,简单代码如下:然后run as junit test就可以运行了public class IntegerTest {@Testpublic void test() {Integer i1 = new Integer(2) ;Integer i2 = new Integer(2) ;Integer i3 = 35;Integer i4 = 35;System.out.println(i3==i4);}}。
2.如何用googletest写单元测试
Xcode中集成了单元测试框架OCUnit,可以完成不同侧重点的测试。
Xcode下的单元测试分为logicuinttests和applicationunittests,两种类型的单元测试都需要对应一个自己的Target。logicuinttests在编译阶段进行,并且只能在模拟器中进行,并且需要配置一个单独的schemes来运行。
主要是针对数据层的各个模块进行测试,如果数据层的模块划分比较理想解耦相对彻底,则可以通过逻辑单元测试对各模块给出各种输入,然后对各数据模块的输出进行判断,来判断各数据模块是否正常。applicationunittests在程序运行阶段进行,可以在模拟器和真机上进行,可以在应用的schemes或者单独配置的schemes里面运行。
主要是针对应用中的相对比较重要的类以及部分简单的界面操作进行测试,完成逻辑单元测试以外的检测。xcode可以通过2种方式创建UnitTest,一种是创建工程时自带UnitTest,一种时在已有工程添加UnitTest。
三.Xcode创建带UnitTest的工程(Xcode4.6.2)如果在新建工程的时候选中并且新建的工程是一个应用,那么系统会默认生成applicationunittests;新建其它类型的工程选中IncludeUnitTests的话,系统默认生成logicuinttests。系统自动生成的测试单元时会自动生成对应的target,并且一个target只能对应一中类型的单元测试,但可以包含多个测试文件,针对工程中不同的类进行测试。
四.Xcode向已有工程添加UnitTest(Xcode4.6.2)如果在新建工程的时候没有选择IncludeUnitTests的话,则可以手动去添加单元测试。下面首先介绍下向工程添加单元测试的target:1、选则File->New->NewTarget,在左侧栏中选中iOS->other,右边选择CocoaTouchUnitTestsBundle,如图Next后位target完成命名这样就完成了向工程中添加单元测试用的target。
效果如下其实按如上步骤添加单元测试target的话,生成的就是一个logicuinttests。一般情况下Xcode在你添加新的target的时候会自动的添加一个schemes,该schemes的命名与你添加的target一样。
如果你不想在新建target的时候新建scheme(因为applicationunittests可以在真机和模拟器上运行,并且时在程序运行时进行测试,所以完全可以和应用本身的target共用一个scheme,这样进行应用单元测试的时候就不用切换scheme。)可以选择上图的ManageSchemes,去掉Autocreateschemes,如下图:为了验证新的scheme已经加入了新建的target,对这个新添加的scheme进行编辑,你可以在选中左侧的Test后看到右边显示出新添加的LogicUnitTestsTarget,说明该scheme已经默认添加了新添加的Target。
新建完target后,可以将新建的target设置成logicunittests或者applicationunittests:设置logicunittests:就像之前所说,如果按上面的步骤添加一个单元测试用的target的话,就已经默认配置成了一个logicunittests。为了确认新建的logicuinttests配置正确,可以进行如下的验证:1、选择新建的scheme:LogicUnitTests和一个运行目标2、选择Product->Test(或者Command+U)3、Xcode会显示BuildSuccessed和Testfailed,选择View->Navigators->Issue(或者Command+4)会有下图中的结果选择View->Navigators->ShowLogNavigator(Command+7)这样就说明添加的逻辑单元测试运行正确,因为没有添加测试语句,只有一个STFail,所以报错。
将LogicUnitTest设置applicationunittests:如果在新建工程的时候选择Includeunittests,则系统会默认生成一个测试target,并配置为applicationunittests。如果新建工程时没有选择包含单元测试,则在新建LogicUnitTest单元测试的Target之后,可以按如下步骤配置applicationunittests:1、选择新建的LogicUnitTest单元测试target,并选择BuildSetting栏和“All”2、在searchbar里面输入BundleLoader,将其值设置为$(BUILT_PRODUCTS_DIR)/.app/效果如下3、搜索TestHost,设置其值为$(BUNDLE_LOADER)效果如图4、使新建的单元测试target依赖与编译应用的target,效果如图5、如果是在新建的时候系统默认新建了scheme,则可以通过新的scheme来进行applicationtests,如果没有默认新建scheme,则可以编辑用来编译工程的scheme,选择左侧的Test如图:点击底部的“+”,将单元测试的target添加进来。
如图这样原来的logicunittests就配置成applicationunittests了。可以按运行logicunittests的方法运行applicationunittests,来验证是否配置正确。
选择Product->Test(或者Command+U)可以对比LogicUnitTest运行完的report和ApplicationUnitTest运行的report有不一样的地方,就是上图方框这一栏ApplicationUnitTest。
3.如何做单元测试
单元测试步骤:
在代码编写完成后的单元测试工作主要分为两个步骤:人工静态检查和动态执行跟踪。
人工静态检查是测试的第一步,这个阶段工作主要是保证代码算法的逻辑正确性(尽量通过人工检查发现代码的逻辑错误)、清晰性、规范性、一致性、算法高效性。并尽可能的发现程序中没有发现的错误。
第二步是通过设计测试用例,执行待测程序来跟踪比较实际结果与预期结果来发现错误。经验表明,使用人工静态检查法能够有效的发现30%到70%的逻辑设计和编码错误。但是代码中仍会有大量的隐性错误无法通过视觉检查发现,必须通过跟踪调试法细心分析才能够捕捉到。所以,动态跟踪调试方法也成了单元测试的重点与难点。
4.请问大神,用J
googletest是一个用来写C++单元测试的框架,它是跨平台的,可应用在windows、linux、Mac等OS平台上。
下面,我来说明如何使用最新的1.6版本gtest写自己的单元测试。本文包括以下几部分:1、获取并编译googletest(以下简称为gtest);2、如何编写单元测试用例;3、如何执行单元测试。
4、google test内部是如何执行我们的单元测试用例的。1. 获取并编译gtestgtest试图跨平台,理论上,它就应该提供多个版本的binary包。
但事实上,gtest只提供源码和相应平台的编译方式,这是为什么呢?google的解释是,我们在编译出gtest时,有些独特的工程很可能希望在编译时加许多flag,把编译的过程下放给用户,可以让用户更灵活的处理。这个仁者见仁吧,反正也是免费的BSD权限。
5.junit怎么写
下面是我做项目时的例子,希望对你有所帮助。
/* *@author ougaoyan ,date:2008-10-19 */ package test; import java.util.Date; import junit.framework.TestCase; import app.DA.BookDA; import app.PD.Book; public class TestBookDA extends TestCase { public TestBookDA(String name){ super(name); } // public Book(int bookID, String cip, String name, String author,String press, String category, int quantity, int reborrowable,int borrowerID,Date startDate) public void testEditBook(){ Book book1 = new Book(1,"123456","信号","张建","某出版社","电信",1,1,1,new Date()); Book book2 = new Book(-1,"123456","信号","张建","某出版社","电信",1,1,1,new Date()); Book book3 = new Book(99,"123456","信号","张建","某出版社","电信",1,1,1,new Date()); Book book4 = new Book(1,"123456","信号","张建","某出版社","电信",1,1,1,new Date()); assertEquals(true,BookDA.editBook(book1)); assertEquals(false,BookDA.editBook(book2)); assertEquals(true,BookDA.editBook(book3)); assertEquals(false,BookDA.editBook(book4)); } ////Book(int borrowerID, Date startDate, int reBorrowable, String cip) public void testAddBook(){ Book book1 = new Book(0,"234567","信发号","张建","某出版社","电信",1,1,1,new Date()); Book book2 = new Book(0,"123456","信的号","张建","某出版社","电信",1,1,1,new Date()); Book book3 = new Book(0,"99999","信i号","张建","某出版社","电信",1,1,1,new Date()); assertEquals(true,BookDA.addBook(book1)); assertEquals(true,BookDA.addBook(book2)); assertEquals(false,BookDA.addBook(book3)); } public static void main(String[] args) { junit.textui.TestRunner.run(TestBookDA.class); System.out.println(new TestBookDA("TestBookDA").countTestCases()); } } /* *@author ougaoyan ,date:2008-10-19 */ package test; import java.util.Date; import java.util.Vector; import junit.framework.TestCase; import app.DA.CipDA; import app.PD.Cip; public class TestCipDA extends TestCase { public TestCipDA (String name){ super(name); } public void testFindBooksByName(){ String name1 = "数据库"; String name2 = "小小"; Vector vector1 = CipDA.findBooksByName(name1); Vector vector2 = CipDA.findBooksByName(name2); assertNotNull(vector1); assertNull(vector2); } public void testFindBooksByAuthor(){ String name1 = "欧阳"; String name2 = "小小"; Vector vector1 = CipDA.findBooksByAuthor(name1); Vector vector2 = CipDA.findBooksByAuthor(name2); assertNotNull(vector1); assertNull(vector2); } public void testFindBooksByCategory(){ String name1 = "计算机"; String name2 = "计 算 机"; String name3 = "wucimin"; Vector vector1 = CipDA.findBooksByCategory(name1); Vector vector2 = CipDA.findBooksByCategory(name2); Vector vector3 = CipDA.findBooksByCategory(name3); assertNotNull(vector1); assertNotNull(vector2); assertNull(vector3); } // public void testEditCip(){ // } //public Cip(String cip, String name, String author, String press,String category, int quantity, int reserverID,Date reservedDate) public void testAddCip(){ Cip cip1 = new Cip("2244","新加书","新者","出版社","计算机",3,0,new Date()); //Cip cip2 =new Cip(null,"新加书","新者","出版社","计算机",3,0,new Date()); Cip cip3 =new Cip("22er3rr3rt4t43t43t3t34t34t34t44","新加书","新者","出版社","计算机",3,0,new Date()); assertEquals(true,CipDA.addCip(cip1)); //assertEquals(false,CipDA.addCip(cip2)); assertEquals(false,CipDA.addCip(cip3)); } public void testEditCip(){ Cip cip1 = new Cip("2244","新加书","新者","出版社","计算机",3,0,new Date()); Cip cip2 =new Cip(null,"新加书","新者","出版社","计算机",3,0,new Date()); Cip cip3 =new Cip("22er3rr3rt4t43t43t3t34t34t34t44","新加书","新者","出版社","计算机",3,0,new Date()); assertEquals(true,CipDA.editCip(cip1)); assertEquals(false,CipDA.editCip(cip2)); assertEquals(false,CipDA.editCip(cip3)); } public static void main(String[] args) { junit.textui.TestRunner.run(TestCipDA.class); System.out.println(new TestCipDA("TestCipDA").countTestCases()); } } /* *@author ougaoyan ,date:2008-10-19 */ package test; import java.util.Date; import java.util.Vector; import junit.framework.TestCase; import app.DA.LibManagerDA; import app.PD.LibManager; import app.PD.Student; public class TestLibManagerDA extends TestCase { public TestLibManagerDA(String name){ super(name); } public void testCheck()。
转载请注明出处育才学习网 » unittest怎么写