如何编写测试类来测试我的代码?
我想一个测试类测试这个类,但我不知道怎么写呢,我试着在网上看到,但我仍想不出看着办吧out.I写了一篇关于BlueJ的代码,我试图创建一套游戏。如何编写测试类来测试我的代码?
import java.util.*;
public class Deck
{
ArrayList<Card> deck;
public Deck()
{
deck = new ArrayList<Card>();
}
public Deck (int capacity)
{
deck = new ArrayList<Card>(capacity);
}
public int getNumCards()
{
return deck.size();
}
public boolean isEmpty()
{
return deck.isEmpty();
}
public void add (Card card)
{
deck.add(0,card);
}
public Card takeTop()
{
return deck.remove(0);
}
public void shuffle()
{
Collections.shuffle(deck);
}
public void sort()
{
Collections.sort(deck);
}
public String toString()
{
return (deck.toString()+ "\n");
}
}
您需要创建测试类的功能的主要方法。
public static void main(String args[])
{
//To do
}
在你的主要方法中,你需要例如构造一个Card对象(假设你有Card类)。
Card card = new Card();
那么你还需要构建一个甲板对象,你会用它来调用甲板类的方法,以便例如添加卡到甲板
Deck deck = new Deck();
使用甲板对象调用add方法将卡添加到甲板
deck.add(card);
所以,现在你的主要方法应该是这个样子:
public static void main(String args[])
{
Card card = new Card();
Deck deck = new Deck();
deck.add(card);
}
同样在你的Deck班,我推荐使用List<Card> deck = new ArrayList<Card>();
而不是ArrayList<Card> deck = new ArrayList<Card>();
。
希望这给你一个起点。
首先,您需要确定需要为您的课程编写哪些测试用例, 您可以使用像Junit这样的库来创建测试用例,方便您使用测试用例列表。
下面是一些Junit的方法
import static org.junit.Assert.assertEquals;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class MyClassTest {
MyClass tester;
@BeforeClass
public static void testSetup() {
tester = new MyClass();
}
@AfterClass
public static void testCleanup() {
// Do your cleanup here like close URL connection , releasing resources etc
}
@Test(expected = IllegalArgumentException.class)
public void testExceptionIsThrown() {
tester.divide(1000, 0);
}
@Test
public void testMultiply() {
assertEquals("Result", 50, tester.multiply(10, 5));
}
}
通过将它拖入@Before或类似的东西来删除冗余和初始化。命名正在测试'剪切'的对象是一个很好的约定,imo。 – atamanroman 2013-03-08 19:55:15
使用一个测试框架Junit的类似的例子,请参见下面的示例,
public class ThingTester extends TestCase
{
public ThingTester (String name)
{
super (name);
}
public static void main(String[] args)
{
TestRunner.runAndWait(new TestSuite(ThingTester.class));
}
public void testGetName() throws Exception
{
String fileSpec = new String("c:xxxyyyzzz.txt");
assertEquals("zzz.txt", getName(fileSpec));
}
}
我想我不明白你想要什么,但我会以任何方式提出我的建议。
哪里是卡类?
在你的卡类添加这个方法,编译代码并执行。
public static void main(String[] args) { Deck deck = new Deck(); // Call your methods here and do what do you want... }
这也是*驱动程序*。你必须断言一个结果。 – atamanroman 2013-03-08 20:00:19
你是激励我们去尝试帮助某人的原因,因为你没有任何理由就冷静下来......我知道什么是测试课,但是问题的本身可能不是,所以让他选择他想要的并且投票得更好回答... – 2013-03-08 20:27:40
如果你在蓝鸟,你可以在类,并在弹出的底部,只需右键单击 - 时,将会有“创建测试类”的选项。使用这将简化过程。下面我提供了一个Blue Jay创建的例子。
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* The test class TestOfClass2Test.
*
* @author (your name)
* @version (a version number or a date)
*/
public class TestOfClass2Test
{
/**
* Default constructor for test class TestOfClass2Test
*/
public TestOfClass2Test()
{
}
/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
@Before
public void setUp()
{
}
/**
* Tears down the test fixture.
*
* Called after every test case method.
*/
@After
public void tearDown()
{
}
}
测试框架是有原因的。你的课是一个*驱动*,测试意味着要声明一个结果。 – atamanroman 2013-03-08 19:57:50
我觉得如果他不知道如何为自己的程序创建一个测试课,那么他就不会觉得OP对他有好处。从OP描述中我认为他需要一个驾驶员级来测试他的程序。 – 2013-03-08 19:59:10