锁定老帖子 主题:对junit的一点体会
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2004-02-12
potian 写道 jaqwolf 写道 使用junit测试,代码量几乎增加一倍,而且感觉不是所有的地方都测试到了。可能是测试用力写的不好。
总之现在我又回到用main来测试了。 如果我没判断错误的话,你是先写好所有的代码逻辑再写测试的,这不但会重复你所有的调试过程,渐进设计过程,而且极易产生厌倦心理。 Main测试估计就是敷衍了事,开始可能认真一点,以后就会越来越少 如果你没办法一下子TDD,那么就做到写几句代码马上测试,拖的时间越长,厌倦心里就越强烈,测试的作用也越不明显,甚至基本上写不出多少有用的测试。 除了功能测试,我从来不为单元测试正式写测试用例,测试是在编码过程中逐渐完善起来的。 确实,如果全写完了再测试,或者是加文档,很容易做不下去。尤其是文档,如果随着代码一起写,还可以捕捉当时的思路。用main测试基本上是敷衍了事。如果测试本身不复杂,或者只是向同事们做一个演示的话还可以,如果真的是用来进行单元测试,则远远不够。 |
|
返回顶楼 | |
发表时间:2004-02-12
robbin 写道 giantzou 写道 Junit是自己写的,如果写得不过严格,或想得不够周到,不知有没有可能变成Pandora's box , 说到底还是得靠自己的Coding Ability,不知大家有何高见呢?
其实我觉得单元测试很难写的,特别是涉及到对数据库的操作,setup和teardown方法怎么写,是颇费脑筋的,有时候写unit test感觉到比写代码还要费事。 Please use *DbUnit* or MockObjects. They work fine and fast! |
|
返回顶楼 | |
发表时间:2004-02-13
hijack 写道 robbin 写道 giantzou 写道 Junit是自己写的,如果写得不过严格,或想得不够周到,不知有没有可能变成Pandora's box , 说到底还是得靠自己的Coding Ability,不知大家有何高见呢?
其实我觉得单元测试很难写的,特别是涉及到对数据库的操作,setup和teardown方法怎么写,是颇费脑筋的,有时候写unit test感觉到比写代码还要费事。 Please use *DbUnit* or MockObjects. They work fine and fast! 为什么用MockObject,为什么不用Hsql的内存模式,我想不出任何理由? 并且我也看不出来,使用了Hibernate以后,DbUnit能够起到多大作用,数据存取抽象的问题在Hibernate相关的项目中不是很有用,我上面讲的测试用例继承层次是和特定的应用程序的特定层次的特定包新关的 |
|
返回顶楼 | |
发表时间:2004-02-13
potian 写道 jaqwolf 写道 使用junit测试,代码量几乎增加一倍,而且感觉不是所有的地方都测试到了。可能是测试用力写的不好。
总之现在我又回到用main来测试了。 如果我没判断错误的话,你是先写好所有的代码逻辑再写测试的,这不但会重复你所有的调试过程,渐进设计过程,而且极易产生厌倦心理。 Main测试估计就是敷衍了事,开始可能认真一点,以后就会越来越少 如果你没办法一下子TDD,那么就做到写几句代码马上测试,拖的时间越长,厌倦心里就越强烈,测试的作用也越不明显,甚至基本上写不出多少有用的测试。 除了功能测试,我从来不为单元测试正式写测试用例,测试是在编码过程中逐渐完善起来的。 。。。。看来有必要反省一下,重新使用junit。 |
|
返回顶楼 | |
发表时间:2004-02-16
不知对JavaBean作测试用junit会不会合适呢?
我发现了这个东东 http://javabeantester.sourceforge.net/ 有没有人用过呢?还是有更好的方法测试bean? |
|
返回顶楼 | |
发表时间:2004-04-16
giantzou 写道 不知对JavaBean作测试用junit会不会合适呢?
我发现了这个东东 http://javabeantester.sourceforge.net/ 有没有人用过呢?还是有更好的方法测试bean? 推荐一本书junit in action 你们所说的这些问题,这本书应该都有讲的。 在这里贴一下它的目录: PART 1 JUNIT DISTILLED............................................... 1 JUnit jumpstart 1.1 Proving it works 1.2 Starting from scratch 1.3 Understanding unit testing frameworks 1.4 Setting up JUnit 1.5 Testing with JUnit 1.6 Summary 2 Exploring JUnit 2.1 Exploring core JUnit 2.2 Launching tests with test runners Selecting a test runner 20 Defining your own test runner 2.3 Composing tests with TestSuite 21 Running the automatic suite 22 Rolling your own test suite 2.4 Collecting parameters with TestResult 2.5 Observing results with TestListener 2.6 Working with TestCase Managing resources with a fixture 29 Creating unit test methods 30 2.7 Stepping through TestCalculator 32 Creating a TestSuite 33 Creating a TestResult 35 Executing the test methods 36 Reviewing the full JUnit life cycle 37 2.8 Summary 38 3 Sampling JUnit 39 3.1 Introducing the controller component 40 Designing the interfaces 41 Implementing the base classes 3.2 Let’s test it! Testing the DefaultController 46 Adding a handler 46 Processing a request 50 Improving testProcessRequest 54 3.3 Testing exception-handling 55 Simulating exceptional conditions 56 Testing for exceptions 60 3.4 Setting up a project for testing 62 3.5 Summary 64 4 Examining software tests 65 4.1 The need for unit tests 66 Allowing greater test coverage 67 Enabling teamwork 67 Preventing regression and limiting debugging 67 Enabling refactoring 68 Improving implementation design 69 Serving as developer documentation 69 Having fun 70 4.2 Different kinds of tests 71 The four flavors of software tests 71 The three flavors of unit tests 75 4.3 Determining how good tests are 77 Measuring test coverage 78 Generating test coverage reports 79 Testing interactions 81 4.4 Test-Driven Development 81 Tweaking the cycle 81 The TDD two-step 83 4.5 Testing in the development cycle 84 4.6 Summary 87 5 Automating JUnit 88 5.1 A day in the life 89 5.2 Running tests from Ant 90 Ant, indispensable Ant 91 Ant targets, projects, properties, and tasks 92 The javac task 94 The JUnit task 96 Putting Ant to the task 97 Pretty printing with JUnitReport 98 Automatically finding the tests to run 100 5.3 Running tests from Maven 102 Maven the goal-seeker 102 Configuring Maven for a project 104 Executing JUnit tests with Maven 109 Handling dependent jars with Maven 109 5.4 Running tests from Eclipse 112 Creating an Eclipse project 112 Running JUnit tests in Eclipse 114 5.5 Summary 116 PART 2 TESTING STRATEGIES .................................... 117 6 Coarse-grained testing with stubs 119 6.1 Introducing stubs 120 6.2 Practicing on an HTTP connection sample 121 Choosing a stubbing solution 124 Using Jetty as an embedded server 125 6.3 Stubbing the web server’s resources 126 Setting up the first stub test 126 Testing for failure conditions 132 Reviewing the first stub test 133 6.4 Stubbing the connection 134 Producing a custom URL protocol handler 134 Creating a JDK HttpURLConnection stub 136 Running the test 137 6.5 Summary 138 7 Testing in isolation with mock objects 139 7.1 Introducing mock objects 140 7.2 Mock tasting: a simple example 141 7.3 Using mock objects as a refactoring technique 146 Easy refactoring 147 Allowing more flexible code 148 7.4 Practicing on an HTTP connection sample 150 Defining the mock object 150 Testing a sample method 151 Try #1: easy method refactoring technique 152 Try #2: refactoring by using a class factory 155 7.5 Using mocks as Trojan horses 159 7.6 Deciding when to use mock objects 163 7.7 Summary 164 8 In-container testing with Cactus 165 8.1 The problem with unit-testing components 166 8.2 Testing components using mock objects 167 Testing the servlet sample using EasyMock 168 Pros and cons of using mock objects to test components 170 8.3 What are integration unit tests? 172 8.4 Introducing Cactus 173 8.5 Testing components using Cactus 173 Running Cactus tests 174 Executing the tests using Cactus/Jetty integration 174 Drawbacks of in-container testing 178 8.6 How Cactus works 179 Executing client-side and server-side steps 180 Stepping through a test 180 8.7 Summary 182 9 Unit-testing servlets and filters 187 9.1 Presenting the Administration application 188 9.2 Writing servlet tests with Cactus 189 Designing the first test 190 Using Maven to run Cactus tests 192 Finishing the Cactus servlet tests 198 9.3 Testing servlets with mock objects 204 Writing a test using DynaMocks and DynaBeans 205 Finishing the DynaMock tests 206 9.4 Writing filter tests with Cactus 208 Testing the filter with a SELECT query 209 Testing the filter for other query types 210 Running the Cactus filter tests with Maven 212 9.5 When to use Cactus, and when to use mock objects 213 9.6 Summary 214 10 Unit-testing JSPs and taglibs 215 10.1 Revisiting the Administration application 216 10.2 What is JSP unit testing? 217 10.3 Unit-testing a JSP in isolation with Cactus 217 Executing a JSP with SQL results data 218 Writing the Cactus test 219 Executing Cactus JSP tests with Maven 222 10.4 Unit-testing taglibs with Cactus 224 Defining a custom tag 225 Testing the custom tag 227 Unit-testing tags with a body 228 Unit-testing collaboration tags 233 10.5 Unit-testing taglibs with mock objects 233 Introducing MockMaker and installing its Eclipse plugin 234 Using MockMaker to generate mocks from classes 234 10.6 When to use mock objects and when to use Cactus 237 10.7 Summary 237 11 Unit-testing database applications 239 11.1 Introduction to unit-testing databases 240 11.2 Testing business logic in isolation from the database 242 Implementing a database access layer interface 243 Setting up a mock database interface layer 244 Mocking the database interface layer 246 11.3 Testing persistence code in isolation from the database 247 Testing the execute method 248 Using expectations to verify state 256 11.4 Writing database integration unit tests 260 Filling the requirements for database integration tests 260 Presetting database data 261 11.5 Running the Cactus test using Ant 265 Reviewing the project structure 265 Introducing the Cactus/Ant integration module 266 Creating the Ant build file step by step 267 Executing the Cactus tests 274 11.6 Tuning for build performance 275 Factoring out read-only data 275 Grouping tests in functional test suites 277 Using an in-memory database 278 11.7 Overall database unit-testing strategy 278 Choosing an approach 278 Applying continuous integration 279 11.8 Summary 280 12 Unit-testing EJBs 281 12.1 Defining a sample EJB application 282 12.2 Using a fa&ade strategy 283 12.3 Unit-testing JNDI code using mock objects 284 12.4 Unit-testing session beans 285 Using the factory method strategy 289 Using the factory class strategy 293 Using the mock JNDI implementation strategy 297 12.5 Using mock objects to test message-driven beans 307 12.6 Using mock objects to test entity beans 310 12.7 Choosing the right mock-objects strategy 312 12.8 Using integration unit tests 313 12.9 Using JUnit and remote calls 314 Requirements for using JUnit directly 315 Packaging the Petstore application in an ear file 315 Performing automatic deployment and execution of tests 319 Writing a remote JUnit test for PetstoreEJB 325 Fixing JNDI names 326 Running the tests 327 12.10 Using Cactus 328 Writing an EJB unit test with Cactus 328 Project directory structure 329 Packaging the Cactus tests 329 Executing the Cactus tests 333 12.11 Summary 334 A The source code 335 A.1 Getting the source code 336 A.2 Source code overview 336 A.3 External libraries 338 A.4 Jar versions 339 A.5 Directory structure conventions 340 B Eclipse quick start 341 B.1 Installing Eclipse 342 B.2 Setting up Eclipse projects from the sources 342 B.3 Running JUnit tests from Eclipse 343 B.4 Running Ant scripts from Eclipse 344 B.5 Running Cactus tests from Eclipse 345 |
|
返回顶楼 | |
发表时间:2004-04-17
where to get it?
|
|
返回顶楼 | |
发表时间:2004-04-18
airport 写道 where to get it?
咳咳,这个嘛,国内好像还没有哎。 而且本站也严禁交流。。。书籍,不过有两个样章,可以看看,去manning找找应该可以找着,其中有一章是讲mock object的。 还有推荐 http://groups.yahoo.com/group/junit/ 该groups里面经常会有很多专家出现哦,包括Vincent Massol , 他是junit in action的作者 相信如果把上面的文章看过一遍(很多啊,看完的话需要很大的耐心, 不过,应该会有收获,偶最先的时候就是看这上面的) 还有就是chinaxp网站的源代码里有相应的测试程序,别人有没有收获 我不知道,但是我自己照着那上面的学习,感觉还是很有收获的。 同事搜索一下,还可以找到测试驱动开发的groups,是那本书的作者建的groups,当那本书还处于草稿阶段的时候,我就是在那里下到它的。 再有就是junit的源代码,主要是junit.extensions 和junit.framework 这两个包看明白了,自己再实际的写写例子。 对于单元测试,我写的不多,但是也没有什么难的,开头的时候比较难,坚持下来的话,会上瘾的,最好联系测试驱动那套东西来学习就更好了,反正我是不愿意对已经写好的代码添加它的单元测试代码的。 感觉是,junit写单元测试的时候主要的难点是要模拟一个尽量简单的测试环境(fixture),这个环境本身应该要求不需要写测试代码就认为是合理的,所以越简单越好。 我自己也只是私下里用junit和jemmy结合起来写过一些客户端应用程序(有界面的)的单元测试代码,对于server端的没什么实际体会,但我想基本的道理都是一样,只是选择和构造的fixture不一样吧。 [/i] |
|
返回顶楼 | |
发表时间:2004-04-27
针对数据库的单元测试,大家可以看一下dbunit,它是junit的一个扩展。
设计思想是,首先提供参考测试数据,可以是从测试库中导出的数据,也可以是自己是手工维护的数据。然后在单元测试的setup和dearDown时自动用一定的规则重新将前面的参考测试数据和真实测试库中的数据做一些处理。比如说完全同步,或是将新增的数据删除等等。 这样,我们在做基于数据库的单元测试时总是可以得到一个可预测的测试数据。 www.dbunit.org 我们在做单元测试的时候就不用再费心的考虑如何setup和tearDown了。 哇,写的乱乱的,不晓得各位有没有看明白。 |
|
返回顶楼 | |
发表时间:2004-05-08
giantzou 写道 Junit是自己写的,如果写得不过严格,或想得不够周到,不知有没有可能变成Pandora's box , 说到底还是得靠自己的Coding Ability,不知大家有何高见呢?
用mock了,写unit test的时候,一定要注意你是在写隔离测试,一个unit一个unit测试,则是A unit的时候要假设 所有其他的unit都正确(用mock), 这样的代码不会很多了。 否则你就是在写function测试了, 当然费事了。 |
|
返回顶楼 | |