简体   繁体   English

使用测试运行器进行两种不同的测试JUnit

[英]Using test runners for two different tests JUnit

I have a class that I am testing using JUnitCore Test Runners to run the test. 我有一个类,我正在测试使用JUnitCore Test Runners来运行测试。 The program essentially removes every nth value from a linked list, it is very simple. 该程序基本上从链表中删除每个第n个值,这非常简单。 I am testing: 我正在测试:

1) See if the new list size is the size I expect it to be. 1)查看新列表大小是否是我期望的大小。 2) See if an element was removed that I know should have been. 2)看看我知道应该删除的元素是否已删除。 For example below I know that "Item 1" has been removed. 例如下面我知道“项目1”已被删除。

I have been tasked with making sure that 1) will pass and 2) will fail, and all of this works, but on running my code, my problems are: 我的任务是确保1)将通过,2)将失败,所有这一切都有效,但在运行我的代码时,我的问题是:

1) It is not a very efficient test and could be improved. 1)这不是一个非常有效的测试,可以改进。 2) When 1) is True and 2) is false (as in code below), the output is just "False", and I am not sure if this is right. 2)当1)为True且2)为假(如下面的代码中)时,输出只是“假”,我不确定这是否正确。

I have tried when both classes are true and it returns True. 我已经尝试过两个类都为true并返回True。

  • Code for testing 测试代码

      import org.junit.Test; import org.junit.Ignore; import static org.junit.Assert.*; import java.util.LinkedList; public class PruneTest { @Test public void testListSize() { LinkedList <String> linkedlist = new LinkedList<String>(); linkedlist.add("Item 0"); linkedlist.add("Item 1"); linkedlist.add("Item 2"); linkedlist.add("Item 3"); linkedlist.add("Item 4"); linkedlist.add("Item 5"); linkedlist.add("Item 6"); linkedlist.add("Item 7"); linkedlist.add("Item 8"); linkedlist.add("Item 9"); Pruner prunes = new Pruner(); prunes.prune(linkedlist, 2); assertEquals(5, linkedlist.size()); } @Test public void testItemRemoved(){ LinkedList <String> linkedlist = new LinkedList<String>(); linkedlist.add("Item 0"); linkedlist.add("Item 1"); linkedlist.add("Item 2"); linkedlist.add("Item 3"); linkedlist.add("Item 4"); linkedlist.add("Item 5"); linkedlist.add("Item 6"); linkedlist.add("Item 7"); linkedlist.add("Item 8"); linkedlist.add("Item 9"); Pruner prunes = new Pruner(); prunes.prune(linkedlist, 2); assertTrue(linkedlist.contains("Item 1")); } } 
  • Code for creating Test Runner 用于创建Test Runner的代码

     import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; public class PruneTestTwo { public static void main() { Result result = JUnitCore.runClasses(PruneTest.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println(result.wasSuccessful()); } } 

You can get more verbose and useful output from your tests using the alternative assertThat method with matchers, rather than the older assertEquals / assertTrue 使用替代的assertThat方法和匹配器,而不是旧的assertEquals / assertTrue ,您可以从测试中获得更详细和有用的输出

For example you could combine your two tests above and get meaningful output about what's failing like this: 例如,您可以将上面的两个测试结合起来,并获得有关失败的有意义的输出:

   @Test
   public void testListSize() {

    LinkedList <String> linkedlist = new LinkedList<String>();
    linkedlist.add("Item 0");
    linkedlist.add("Item 1");
    linkedlist.add("Item 2");
    linkedlist.add("Item 3");
    linkedlist.add("Item 4");
    linkedlist.add("Item 5");
    linkedlist.add("Item 6");
    linkedlist.add("Item 7");
    linkedlist.add("Item 8");
    linkedlist.add("Item 9");

    Pruner prunes = new Pruner();
    prunes.prune(linkedlist, 2);

    assertThat(linkedlist.size(), is(5));
    assertThat(linkedlist, hasItem("Item 1"));    
  }

Then when the second assertion fails, you'll get output similar to this (which is much easier to understand than a lonely false ): 然后当第二个断言失败时,你会得到类似于此的输出(这比一个孤独的false更容易理解):

java.lang.AssertionError: 
Expected: a collection containing "x"
     but: was "Item 0", was "Item 1", was "Item 2", was "Item 3", was "Item 4", was "Item 5", was "Item 6", was "Item 7", was "Item 8", was "Item 9"

You could improve it even more by making sure the pruned list is exactly what you expect - instead of checking the size and one item in it, you can check the size and every item (along with the order of the list) by using the contains matcher: 您可以通过确保修剪后的列表完全符合您的预期来改进它 - 而不是检查大小和其中的一个项目,您可以使用contains来检查大小和每个项目(以及列表的顺序)匹配:

assertThat(linkedlist, contains("Item 0", "Item 2", "Item 4", "Item 6", "Item 8"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM