??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲午夜无码AV毛片久久,亚洲中文字幕一区精品自拍,亚洲色www永久网站 http://www.tkk7.com/xixuui/category/15556.htmlzh-cn Tue, 27 Feb 2007 14:25:04 GMT Tue, 27 Feb 2007 14:25:04 GMT 60 如何试自定义断a http://www.tkk7.com/xixuui/archive/2006/11/24/83329.html阿辉 阿辉 Fri, 24 Nov 2006 09:48:00 GMT http://www.tkk7.com/xixuui/archive/2006/11/24/83329.html http://www.tkk7.com/xixuui/comments/83329.html http://www.tkk7.com/xixuui/archive/2006/11/24/83329.html#Feedback 0 http://www.tkk7.com/xixuui/comments/commentRss/83329.html http://www.tkk7.com/xixuui/services/trackbacks/83329.html /** * asserts two String arrays are equal?br /> */ public static void assertEquals(final String[] expected, final String[] actual) { if(expected == null && actual == null) { assertTrue(true); } else if(expected.length != actual.length) { fail("expected length is :" + expected.length + "but was:" + actual.length); } else { for(int i = 0; i < expected.length; i++) { assertEquals("W? + (i + 1) + "个元素不相等", expected[i], actual[i]); } } } 可是又怎么对该Ҏ(gu)q行试呢? ]]> Junit4功能 先睹为快. (译文) Q{Q?/title> http://www.tkk7.com/xixuui/archive/2006/09/22/71246.html阿辉 阿辉 Fri, 22 Sep 2006 01:44:00 GMT http://www.tkk7.com/xixuui/archive/2006/09/22/71246.html http://www.tkk7.com/xixuui/comments/71246.html http://www.tkk7.com/xixuui/archive/2006/09/22/71246.html#Feedback 1 http://www.tkk7.com/xixuui/comments/commentRss/71246.html http://www.tkk7.com/xixuui/services/trackbacks/71246.html
An early look at JUnit 4
Upcoming release promises evolution in testing
隑ֺU别: ?/p>
Elliotte Harold
(elharo@metalab.unc.edu ), Adjunct Professor, Polytechnic University
2005-9-15 Q译Q?/p>
原文Q?a >http://www-128.ibm.com/developerworks/java/library/j-junit4.html
JUnit 是JAVA语言事实上的标准试库。JUnit 4是三q以来最具里E碑意义的一ơ发布。它的新Ҏ(gu)主要是针对JAVA5中的标记QannotationQ来化测试,而不是利用子cR反或命名机制。本文将讲述如何使用JUnit 4Q当前前提是你最好具有JUnit的用经验.
JUnit, 由Kent Beck ?Erich Gamma开发,几乎是JAVA开发最重要的第三方工具。正如Martin Fowler 所_(d)“在软g开发领域,从来没有如此少的代码vC(jin)如此重要的作用“。由于JUnitQJAVA代码变得更健壮,更可靠,BUG也比以前更少。由于JUnit (由Smalltalk's的SUnit得来) 的出玎ͼ随后产生?jin)许多xUnit的测试工P如nUnit (.NET), pyUnit (Python), CppUnit (C++), dUnit (Delphi) 和其它不同^台及(qing)语言的测试相关的工具?/p>
虽然JUnit也只是一个工P但其产生的思想和技术却较其架构更意义重大。单元测试,试先行的编E方式,试驱动的开发方式,q必须由JUNIT实现Q也不一定要用SWing实现GUI界面。JUNIT最q的一ơ更新是在三q前Q但它比其它大多数有BUG的框枉要健壮,更重要的是,JAVA一直在改进。现在JAVA支持泛型Q枚举,可变长度参数Q以?qing)标记语aQ开创了(jin)开发可重用框架的新局面)(j)?/p>
JUnit's的停滞不前得那些想要变革的开发h员换其它试工具Q挑战者有Bill Venners的Artima SuiteRunner和Cedric Beust的TestNGQ这些工具库虽然有值得推荐的功能,但没有Q何一ƄC能与JUNIT相比Q没有Q何一Ƒַ兯其它业界产品如Ant, Maven, Eclipseq泛支持Q因此Beck 和Gamma双开始利用JAVA5的新Ҏ(gu)来开发新版的JUNITQ目的是利用JAVA5中的标记Ҏ(gu)得单元测试开发更Ҏ(gu)。Beck_(d)(x)“JUNIT4的主要目的是通过化JUNIT的用鼓励更多的开发h员写更多的测试”。虽然会(x)与以前的版本兼容Q但JUNIT4与从JUNIT1.0开始的版本相比?x)有一个非常大的变化.
注意: 修改基本框架是一把双刃剑Q虽然JUNIT4的目的是清晰的,但细节仍有许多不同,因此本文只是一个简单的介绍Qƈ不是最l文.
试Ҏ(gu)
以前所有版本的JUNIT都用命名机制和反射来定位测试,下面的代码测?+1= 2Q?/p>
import junit.framework.TestCase;
public class AdditionTest extends TestCase {
private int x = 1;
private int y = 1;
public void testAddition() {
int z = x + y;
assertEquals(2, z);
}
}
而在JUNIT 4中,试Ҏ(gu)?strong>@Test 标记说明Q如下:(x)
import org.junit.Test;
import junit.framework.TestCase;
public class AdditionTest extends TestCase {
private int x = 1;
private int y = 1;
@Test public void testAddition() {
int z = x + y;
assertEquals(2, z);
}
}
使用标记的好处是你不用将所有测试方法命名ؓ(f) testFoo()
, testBar(){等?test"开头的Ҏ(gu)Q下面的Ҏ(gu)也同样可以工作:(x)
import org.junit.Test;
import junit.framework.TestCase;
public class AdditionTest extends TestCase {
private int x = 1;
private int y = 1;
@Test public void additionTest() {
int z = x + y;
assertEquals(2, z);
}
}
下面的代码也同样正确Q?/p>
import org.junit.Test;
import junit.framework.TestCase;
public class AdditionTest extends TestCase {
private int x = 1;
private int y = 1;
@Test public void addition() {
int z = x + y;
assertEquals(2, z);
}
}
q种命名机制最大的优点是更适合你的待测试类或方法名Uͼ例如Q你可以使用ListTEst.contains()试 List.contains()
;使用ListTest.addAll()试 List.add(){等Q?/code>
TestCase q可以l用,但你没有必须再扩展ؓ(f)子类Q只要你声明?jin)@TestQ你可以测试方法放在Q何类中,当然如要讉Ka(chn)ssert{方法,你必要引用junit.Assertc,如下Q?/code>
import org.junit.Assert;
public class AdditionTest {
private int x = 1;
private int y = 1;
@Test public void addition() {
int z = x + y;
Assert.assertEquals(2, z);
}
}
你也可以使用JDK5中的新特?static import)使得跟以前版本一L(fng)单:(x)
import static org.junit.Assert.assertEquals;
public class AdditionTest {
private int x = 1;
private int y = 1;
@Test public void addition() {
int z = x + y;
assertEquals(2, z);
}
}
q种Ҏ(gu)试受保护的Ҏ(gu)非常Ҏ(gu)Q因Z可以在测试类中承有受保护方法的c.
SetUp 和TearDown
JUnit 3 ?strong>test runners ?x)在每个试之前自动调?setUp()Ҏ(gu)。此Ҏ(gu)主要用于初始化变量,打开日志Q重|环境变量等。下面是XOM's XSLTransformTest中的
setUp()Ҏ(gu)Q?/code>
protected void setUp() {
System.setErr(new PrintStream(new ByteArrayOutputStream()));
inputDir = new File("data");
inputDir = new File(inputDir, "xslt");
inputDir = new File(inputDir, "input");
}
在JUnit 4中,你仍然可以在每个试前初始化变量和配|环?Q然而,q些操作可以不用在Setup()中完成,你可以在初始化方法前面添?strong>@Beforer 来表C,如下Q?
@Before protected void initialize() {
System.setErr(new PrintStream(new ByteArrayOutputStream()));
inputDir = new File("data");
inputDir = new File(inputDir, "xslt");
inputDir = new File(inputDir, "input");
}
你也可以有多个方法标记有QBeforeQ所有方法都?x)在每个试之前执行Q?
@Before protected void findTestDataDirectory() {
inputDir = new File("data");
inputDir = new File(inputDir, "xslt");
inputDir = new File(inputDir, "input");
}
@Before protected void redirectStderr() {
System.setErr(new PrintStream(new ByteArrayOutputStream()));
}
清除环境与JUNIT3 差不多,在JUNIT3中?tearDown()Ҏ(gu)Q下面的代码是结束测试时回收内存Q?/code>
protected void tearDown() {
doc = null;
System.gc();
}
在JUnit 4中,你还可以使用@After 标记来说明:(x)
@After protected void disposeDocument() {
doc = null;
System.gc();
}
?@Before一P你也可以有多个标记有
@After的清除方法,每个都会(x)在执行完每个试后执行?/code>
最后,你不需要在父类中明调用这些初始化或清除方法.test runner?x)自动调用这些标记的?gu)Q子cM的@BeforeҎ(gu)在父cȝ@BeforeҎ(gu)之后执行Q这与构造函数的执行序一P(j)Q而@AfterҎ(gu)刚好相反Q子cM的@AfterҎ(gu)先执行.然而,多个@Before和@AfterҎ(gu)的执行顺序就是未知的Q?/p>
试集范围的初始?/strong>
JUnit 4中引入了(jin)一JUNIT3没有的新Ҏ(gu),cȝ别的setUp()和tearDown()Q即在一个类的所有测试前执行初始化,q在所有测试完成后执行清除?
例如Q一个测试类中的每个试都要用到一个数据库q接或网l连接,或其它很耗资源初始化或释攄资源Q用不着在每个测试方法前后进行操作,而只需要在试cd始前后执行即可。下面的CZ是用第三方的库q行错误Q在执行所有测试前错误先重定向到非标准输出,然后在所有测试结束后再输出到需要的地方Q这样就不会(x)影响到测试过E中产生的其它信息?/p>
// This class tests a lot of error conditions, which
// Xalan annoyingly logs to System.err. This hides System.err
// before each test and restores it after each test.
private PrintStream systemErr;
@BeforeClass protected void redirectStderr() {
systemErr = System.err; // Hold on to the original value
System.setErr(new PrintStream(new ByteArrayOutputStream()));
}
@AfterClass protected void tearDown() {
// restore the original value
System.setErr(systemErr);
}
上面的操作没有必d每个试前后执行。然而要注意的是Q这U方法可能媄(jing)响测试间的结果,如果一个测试改变了(jin)初始化的对象Q而这个对象可能是其它试的输入,那么试的结果可能不正确Q这U方法将依赖试的顺序ƈ可能引入BUG。当优化试性能Qƈ且当你改q了(jin)配置和基准测试后而仍然很慢时Q如数据库连接或|络问题Q你才需要考虑使用q种Ҏ(gu)。只有这P你才能每天执行多ơ测试?
异常试
异常试是JUNIT4中的最大的改进Q以前异常测试是通过try catch实现Q当抛出异常Ӟ在try的最后添加一条fail()语句实现Q如下:(x)
public void testDivisionByZero() {
try {
int n = 2 / 0;
fail("Divided by zero!");
}
catch (ArithmeticException success) {
assertNotNull(success.getMessage());
}
}
q种Ҏ(gu)不仅隄Q而且造成无论成功或失败,代码覆盖工具都不能执行某些代码.而在JUnit 4中,你可以在要抛出异常的代码中添加标记来声明一个异常是期望的:(x)
@Test(expected=ArithmeticException.class) public void divideByZero() {
int n = 2 / 0;
}
如果没有异常抛出Q上面的试则会(x)p|Q如果你想知道异常的详细信息或其它情况,你还是要使用try catch才行
需要忽略的试
也许你有些测试需要很长时间才能执行完成,q是这个测试应该跑得快Q而是它做的很复杂和很慢的工作造成的.如访问远E网l错误,需要很久才能有反馈Q如果你不想让这U测试破坏你整个试q程Q你可能惌q这个测试.当然也有可能某个试出控制范围而失败.如W3C XInclude试集中自动识别一些JAVA不支持的Unicode代码Qؓ(f)?jin)防止这些测试L通不q,可以使用标记 @Ignore 跌q些,如下Q?/code>
// Java doesn't yet support the UTF-32BE and UTF32LE encodings
@Ignore public void testUTF32BE()
throws ParsingException, IOException, XIncludeException {
File input = new File(
"data/xinclude/input/UTF32BE.xml"
);
Document doc = builder.build(input);
Document result = XIncluder.resolve(doc);
Document expectedResult = builder.build(
new File(outputDir, "UTF32BE.xml")
);
assertEquals(expectedResult, result);
}
test runner不会(x)执行q些试Q但?x)说明这些测试被跌了(jin)。在命o(h)行测试界面中Q字母“I”会(x)表示试跌Q或“E”表C测试失败,而不是用点?"表示成功Q?/p>
$ java -classpath .:junit.jar org.junit.runner.JUnitCore nu.xom.tests.XIncludeTest
JUnit version 4.0rc1
.....I..
Time: 1.149
OK (7 tests)
要注意的是,假设q些试׃某种理由攑֜最开始,如果你以后一直忽略这些测试,那些需要被试的代码可能有问题而不?x)被(g)到。因此忽略测试只是一个(f)时解x法,q不是一个解决Q何问题的真正办法?
旉试
性能试是单元测试中最头疼的问题,JUnit 4也未完全解决此问题, 你可以在JUNIT4的测试方法中d一个时间参数。如果测试时间超q参敎ͼ则测试失败。如下,如果试旉过0.5U,则此试p|Q?/p>
@Test(timeout=500) public void retrieveAllElementsInDocument() {
doc.query("http://*");
}
除基准性能试外,旉试在网l测试方面也很有用.如果一个远端的L或数据当掉或太慢Q你可以跌此测试而不用阻塞在q里Q好的测试集可以在作?jin)一些改动后很快的一遍一遍的执行Q可能一天数十次Q设|一个超时让试更快的执行,下面的示例中如果分析http://www.ibiblio.org/xml 的时间超q2U,则测试失败.
@Test(timeout=2000)
public void remoteBaseRelativeResolutionWithDirectory()
throws IOException, ParsingException {
builder.build("http://www.ibiblio.org/xml");
}
新的断言
JUnit 4 增加?jin)两上断文方法用于比较数l:(x)
public static void assertEquals(Object[] expected, Object[] actual)
public static void assertEquals(String message, Object[] expected,
Object[] actual)
q两个方法采用最直接Ҏ(gu)比较Q如果数l长度相同,且每个对应的元素相同Q则比较成功Q否则不成功Q参Cؓ(f)I的情况也作?jin)考虑Q?
需要补充的地方
JUnit 4是一个非常基本的框架Q还不是以前版本的升U。JUNIT3的开发h员会(x)发现有些功能没有?
最大的特点是没有GUI试界面Q当试正确时是l色条,而出错时U色的,你也可以在Eclipse中集成JUNIT使用Q但JUNIT4既没有AWT也没有SWING的GUI试界面Q?/font>
另一个让人吃惊的是失败(期望错误Q和错误Q未预计的异帔R误)(j)没有明显区别Q在JUNIT3中开发h员可以区分这两种情况Q而在JUNIT4中不行;
最后一个特Ҏ(gu)JUNIT中没有用于徏立一堆测试类?code>suite()Ҏ(gu)Q取而代之的是,采用变长参数传递未知数量的试ltest runner?/code>
没有GUI试界面的确不方便,但其它改变简化了(jin)JUNIT的用,从当前JUNIT的操作手册和FAQ的数量就知道Q而JUNIT4的文将不会(x)需要这么多?
~译和运行JUnit 4
现在JUnit 4q没有发布编译版本,如果想体验版本的乐趣Q则需要从CVS中获取源代码。分支标{是"Version4" (see Resources ).要注意的是大部分文是根据JUNIT3~写的,q未同步更新。需要Java 5才能~译JUnit 4Q因为大量用了(jin)标记Q泛型其其它JDK5中的新特性?
执行试的命令行方式与JUNIT3有点区别Q你现在要?org.junit.runner.JUnitCore
c进行测试,如下Q?
$ java -classpath .:junit.jar org.junit.runner.JUnitCore
TestA TestB TestC...
JUnit version 4.0rc1
Time: 0.003
OK (0 tests)
兼容?/strong>
Beck 和Gamma在努力保持后向和前向兼容性。JUnit 4可以直接q行Ҏ(gu)JUNIT3~写的测试类Q而不用Q何修改,直接各试cȝ全名传递给test runner卛_Qtest runner?x)根据不同的试c调用不同的试框架版本Q?
后向兼容性有炚w?ch),卛_JUNIT3中执行根据JUNIT4写的试c,之所以要q样是因为在一个集成环境如Ecplise中,不需要升U到JUNIT4也可以测试JUNIT4的测试类Q从而避免工具IDE的升U。ؓ(f)?jin)让JUNIT4的测试类在JUNI3中能执行Q你需要一个适配c?code>JUnit4TestAdapter 装JUNIT3的测试类Q如下代码:(x)
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(AssertionTest.class);
}
而JAVA斚wQJUNIT4一点兼Ҏ(gu)都没有Q因为完全依赖于JDK5的新Ҏ(gu),因此不可能在JAVA1.4上面执行JUNIT4?/p>
q有...
JUnit 4q未l束Q还有许多需要补充,如文,现在不推荐将以前的测试类升到JUNIT4。当然JUNIT4的开发速度很快Q其计划也很快会(x)实现QJAVA1.4的开发h员仍然可以用JUNIT3.8Q而用JAVA5的h员可以考虑是否采用JUNIT4?jin),因?f)在特性上更适合?
资源
Pragmatic Unit Testing in Java with JUnit
(Andy Hunt and Dave Thomas, Pragmatic Programmers, 2003): An excellent introduction to unit testing.
JUnit Recipes: Practical Methods for Programmer Testing
(J. B. Rainsberger, Manning, 2004): One of the most widely cited and referenced books on JUnit.
TestNG
: Cedric Beust's framework pioneered the annotation based testing style now used in JUnit 4.
"TestNG makes Java unit testing a breeze " (Filippo Diotalevi, developerWorks, January 2005): An introduction to TestNG.
"Incremental development with Ant and JUnit " (Malcolm Davis, developerWorks, November 2000): Explores the benefits of unit testing, in particular using Ant and JUnit, with code samples.
"Demystifying Extreme Programming: Test-driven programming " (Roy Miller, developerWorks, April 2003): Find out how test-driven programming can revolutionize your productivity and quality as a programmer, and learn the mechanics of writing tests.
"Keeping critters out of your code " (David Carew, et. al., developerWorks, June 2003): Learn how you can use JUnit in conjunction with WebSphere Application Developer.
"Measure test coverage with Cobertura " (Elliotte Rusty Harold, developerWorks, May 2005): Learn to identify untested code and locate bugs with this handy open source tool.
下蝲试版本
JUnit 4
: Download the newest version of JUnit the SourceForge CVS repository; be sure to use the branch tag "Version4."
原文Q?a href="/margiex">http://www.tkk7.com/margiex ]]> 从文件中取测试数据进行单元测?/title> http://www.tkk7.com/xixuui/archive/2006/09/20/70800.html阿辉 阿辉 Wed, 20 Sep 2006 06:40:00 GMT http://www.tkk7.com/xixuui/archive/2006/09/20/70800.html http://www.tkk7.com/xixuui/comments/70800.html http://www.tkk7.com/xixuui/archive/2006/09/20/70800.html#Feedback 0 http://www.tkk7.com/xixuui/comments/commentRss/70800.html http://www.tkk7.com/xixuui/services/trackbacks/70800.html { @SuppressWarnings("unused") String line = ""; FileReader myFileReader = new FileReader("com/liyingcheng/testFiles/TestPopSort.txt");//相对路径 BufferedReader myBufferedReader=new BufferedReader(myFileReader); while((line = myBufferedReader.readLine())!=null) { if(line.startsWith("#")) { continue; } String[] tokens = line.split(","); int[] actual = {Integer.parseInt(tokens[0]),Integer.parseInt(tokens[1]),Integer.parseInt(tokens[2])}; int[] expected = {Integer.parseInt(tokens[3]),Integer.parseInt(tokens[4]),Integer.parseInt(tokens[5])}; testSinglePopSort(expected,actual); } } ]]> junit assert() 使用实例 http://www.tkk7.com/xixuui/archive/2006/09/19/70537.html阿辉 阿辉 Tue, 19 Sep 2006 07:13:00 GMT http://www.tkk7.com/xixuui/archive/2006/09/19/70537.html http://www.tkk7.com/xixuui/comments/70537.html http://www.tkk7.com/xixuui/archive/2006/09/19/70537.html#Feedback 1 http://www.tkk7.com/xixuui/comments/commentRss/70537.html http://www.tkk7.com/xixuui/services/trackbacks/70537.html package com.liyingcheng.netTest;
import com.liyingcheng.net.Sort;
import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import junit.xiaoxuhui.Sum;
public class SortTest extends TestCase { // Sort popObj = new Sort();
public SortTest(String name) { super(name); }
protected void setUp() throws Exception { super.setUp(); }
protected void tearDown() throws Exception { super.tearDown(); }
static public void assertEquals(int[] expected, int[] actual) { for(int i = 0; i < expected.length; i++) { assertEquals(null, expected[i], actual[i]); } }
public void testPopSort() { int[] expected = new int[] {1, 2, 3, 4}; assertEquals(expected, Sort.popSort(new int[] {1, 2, 4, 3})); assertEquals(expected, Sort.popSort(new int[] {1, 2, 3, 4})); assertEquals(expected, Sort.popSort(new int[] {1, 3, 4, 2})); assertEquals(expected, Sort.popSort(new int[] {1, 3, 2, 4})); assertEquals(expected, Sort.popSort(new int[] {2, 1, 4, 3})); assertEquals(expected, Sort.popSort(new int[] {2, 4, 1, 3})); assertEquals(expected, Sort.popSort(new int[] {3, 2, 4, 1})); assertEquals(new int[] {1, 2}, Sort.popSort(new int[] {2, 1})); // assertEquals(new int[]{1,3,2,4},popObj.popSort(new int[]{1,2,4,3})); // assertEquals(new int[]{1,2,3,4},popObj.popSort(new int[]{1,2,4,3})); }
public void testCreateArray() { assertEquals(4, Sort.createArray(4).length); }
public void testGetSum() { assertEquals(5050, Sum.getSum(1, 100)); }
public void testFalse() { assertFalse(false); assertTrue(true); }
public void testIsNull() { String str1 = ""; int[] arr1 = {}; String str2 = null; int[] arr2 = null; assertNotNull(str1); assertNotNull(arr1); assertNull(str2); assertNull(arr2); // assertNull(str); }
public void testNull() { }
public void testNotSame() { String str1 = "123"; String str2 = "123"; String str3 = new String(str1); String str4 = new String("123"); int one = 1; int first = 1; assertSame(one, first); assertSame(str1, str2); assertNotSame(str3, str4); //fail("hahahahahahahah"); /* * assertNotSame(one,first); assertNotSame(str1,str2); */ }
public static Test suite() { TestSuite suite = new TestSuite("Test sort!"); suite.addTestSuite(SortTest.class); return suite; }
}
]]>junit数组比较 http://www.tkk7.com/xixuui/archive/2006/09/18/70350.html阿辉 阿辉 Mon, 18 Sep 2006 09:30:00 GMT http://www.tkk7.com/xixuui/archive/2006/09/18/70350.html http://www.tkk7.com/xixuui/comments/70350.html http://www.tkk7.com/xixuui/archive/2006/09/18/70350.html#Feedback 0 http://www.tkk7.com/xixuui/comments/commentRss/70350.html http://www.tkk7.com/xixuui/services/trackbacks/70350.html /** * Asserts that two int arrays are equal. If they are not an * AssertionFailedError is thrown. */ static public void assertEquals(int[] expected, int[] actual ) { for(int i = 0; i < expected.length; i++) { assertEquals(null, expected[i], actual[i]); } } ]]> TestSuite的?/title> http://www.tkk7.com/xixuui/archive/2006/09/18/70275.html阿辉 阿辉 Mon, 18 Sep 2006 05:59:00 GMT http://www.tkk7.com/xixuui/archive/2006/09/18/70275.html http://www.tkk7.com/xixuui/comments/70275.html http://www.tkk7.com/xixuui/archive/2006/09/18/70275.html#Feedback 0 http://www.tkk7.com/xixuui/comments/commentRss/70275.html http://www.tkk7.com/xixuui/services/trackbacks/70275.html 来看看一个例子:(x)package onlyfun.caterpillar.test; import onlyfun.caterpillar.MathTool; import junit.framework.TestCase; public class MathToolTest extends TestCase { public MathToolTest(String testMethod) { super(testMethod); } public void testGcd() { assertEquals(5, MathTool.gcd(10, 5)); } public static void main(String[] args) { junit.textui.TestRunner.run(MathToolTest.class); } } 在这个例子中Q?zhn)q没有看CQ何的TestSuiteQ事实上Q如果?zhn)没有提供M的TestSuiteQTestRunner?x)自己徏立一个,然後q个 TestSuite?x)用反(reflectionQ自动找出testXXX()Ҏ(gu)?br /> 如果(zhn)要自行生成TestSuiteQ则在承TestCase之後Q提供静态的QstaticQ的suite()Ҏ(gu)Q例如:(x)public static Test suite() { return new TestSuite(MathTool.class); }
如果(zhn)没有提供Q何的TestSuiteQ则TestRunner׃(x)像上面这栯动ؓ(f)(zhn)徏立一个,q找出testXXX()Ҏ(gu)Q?zhn)也可以如下面定?suite()Ҏ(gu)Q?br />public static Test suite() { TestSuite suite = new TestSuite(MathTool.class); suite.addTest(new MathToolTest("testGcd")); return suite; }
JUnitq没有规定?zhn)一定要使用testXXX()q样的方式来命名(zhn)的试Ҏ(gu)Q如果?zhn)要提供自qҎ(gu)Q当然JUnit 鼓励(zhn)用testXXX()q样的方法名Uͼ(j)Q则可以如上撰写Qؓ(f)?jin)要能够使用建构函式提供试?gu)名称Q?zhn)的TestCase必须提供如下的徏构函 式:(x)public MathToolTest(String testMethod) { super(testMethod); }
如果要加入更多的试Ҏ(gu)Q用addTest()可以了(jin)Qsuite()Ҏ(gu)传回一个TestSuite物gQ它?TestCase都实作了(jin)Test介面QTestRunner?x)调用TestSuite上的run()Ҏ(gu)Q然後TestSuite?x)将之委托?TestCase上的run()Ҏ(gu)Qƈ执行每一个testXXX()Ҏ(gu)?br /> 除了(jin)l合TestCase之外Q?zhn)q可以将CTestSuitel合在一P例如Q?br />public static Test suite() { TestSuite suite= new TestSuite(); suite.addTestSuite(TestCase1.class); suite.addTestSuite(TestCase2.class); return suite; }
如此之来Q?zhn)可以一ơ运行所有的试Q而不必个别的q行每一个测试案例,(zhn)可以写一个运行全部测试的L试,而在使用TestRunner时呼?suite()Ҏ(gu)Q例如:(x)junit.textui.TestRunner.run(TestAll.suite());
TestCase与TestSuite都实作了(jin)Test介面Q其q行方式?Command 模式 的一个实例,而TestSuite可以l合CTestSuite或TestCaseQ这?Composite 模式 的一个实例?img src ="http://www.tkk7.com/xixuui/aggbug/70275.html" width = "1" height = "1" /> ]]>
վ֩ģ壺
ëƬѹۿַ |
ëƬպëƬ |
hƵ |
һŮ |
Ůһ |
Ʒٸ30P |
ҹþAAAAAëƬѿ |
ůůձ |
ˬָ߳ëƬѿ |
պһ |
91߲ҹ |
Ʒsuvһ88 |
ƴƬ30ӹƷ
|
ˬִ̼߳Ƶ |
ëƬӰƬ |
ɫ͵ר |
УɫС˵ |
ëƬѲ |
㽶þۺӰ |
AVƬWWW |
þۺϾɫۺ97 |
˸徫Ʒ |
ѹۿһëƬ |
þþƷAV͵ |
պһѲ |
18ڵվ
|
ѿwwwƵ |
2019ҹ |
þþþþþõѲ |
ƷۺϾþһ |
avպavӰ |
ò߹ۿƵ |
ĻۺϾƷһ |
þùƷһ |
պvavaŷva |
߹ۿƵ |
ĻӰԺ |
ʪôýˬƵ |
Ļ |
avƬ߹ۿվ |
ۺƵ |