Calculator JUnit测试用例

Posted CSU迦叶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Calculator JUnit测试用例相关的知识,希望对你有一定的参考价值。

覆盖率

总结

1. 这道题是目前为止我做过的唯一一道,测试用例的覆盖率拖累被测文件覆盖率。因为要捕捉好几种类型的异常。目的是增加变异得分。

2. parseCommand这个函数的输出,其实就是提示要怎么写测试用例。

3. 这一题只需要测Calculator.java这一个类,因为它使用ExpressionParser.java,而后者又继承了MathParser.java,至于其余的七个异常文件,自然都是运算触发的。

待改进

1. 我没有分析异常间的继承关系,或许每个test的类都要expected那个父类异常才安全。

2. 还有个别分支因为不理解意思不知道怎么进入。

代码

package net.mooctest;

import static org.junit.Assert.*;

import org.junit.Test;

public class CalculatorTest 
	private Calculator c = new Calculator();

  @Test(timeout = 4000)
  public void test00()  throws Throwable  
      c.expParser = new ExpressionParser(10);
      c.expParser.addVariable("wxy", "330");
      assertEquals(1,c.expParser.numberOfVars);
      c.expParser.addVariable("wyc", "1225");
      c.expParser.addVariable("wyc", "1215");
      assertEquals(2,c.expParser.numberOfVars);
      c.expParser.addVariable("fd", "429");
      assertEquals(3,c.expParser.numberOfVars);
      c.expParser.addVariable("wlw", "918");
      
//      c.parseCommand("list vars");
  
  
  @Test(timeout = 4000)
  public void test01()  throws Throwable  
	  
	  assertEquals("200.0",c.evaluate("100+100"));
	  assertEquals(4,c.expParser.numberOfVars);
	  c.evaluate("gjr = 922");
	  assertEquals(5,c.expParser.numberOfVars);
  
  
  @Test(timeout = 4000,expected=ExpressionParserException.class)
  public void test02()  throws Throwable  
	  c.evaluate("");
  
  
  @Test(timeout = 4000,expected=ExpressionParserException.class)
  public void test03()  throws Throwable  
	  System.out.println(c.evaluate("wxy + wyc"));
  
  
  @Test(timeout = 4000)
  public void test04()  throws Throwable  
	  assertEquals("35.0",c.evaluate("(3+4)*5"));
  
  
  @Test(timeout = 4000,expected=ExpressionParserException.class)
  public void test05()  throws Throwable  
	  assertEquals("1024.0",c.evaluate(" 2^10 "));
  
  
  @Test(timeout = 4000,expected=ExpressionParserException.class)
  public void test06()  throws Throwable  
	  
	  assertEquals("1545.0",c.evaluate("<wxy> + <wyc>"));
	  assertEquals("2.0",c.evaluate("1 + abs[2 - 3]"));
	  assertEquals("1.0",c.evaluate("log[exp[1.0]]"));
	  
	  c.expParser.addVariable("pi", "3.1415926");
	  assertEquals("179.99999692953102",c.evaluate("deg[ <pi> ]  "));
	  assertEquals("3.141592653589793",c.evaluate("rad[ 180 ]"));
	  c.expParser.addVariable("pi", "3.141592653589793");
	  assertEquals("1.0",c.evaluate("sin[ <pi>/2 ]"));
	  assertEquals("6.123233995736766E-17",c.evaluate("cos[ <pi>/2 ]"));
	  assertEquals("-1.2246467991473532E-16",c.evaluate("tan[ <pi> ]"));
	  assertEquals("Infinity",c.evaluate("ctn[ 0 ]"));
	  
  
  
  @Test(timeout = 4000,expected=ExpressionParserException.class)
  public void test07()  throws Throwable  
	  assertEquals("1.0",c.evaluate("fct[ 0 ]"));
	  assertEquals("1.0",c.evaluate("fct[ 1 ]"));
	  assertEquals("3628800.0",c.evaluate("fct[ 10 ]"));
	  assertEquals("6.0",c.evaluate("13%7"));
	  assertEquals("6.0",c.evaluate("13-7"));
	  assertEquals("1.4142135623730951",c.evaluate("csc[ <pi>/4 ]"));
	  assertEquals("1.414213562373095",c.evaluate("sec[ <pi>/4 ]"));
	  
	  
	  System.out.println(c.evaluate("sec[ <pi>/4 ]"));
  
  
  @Test(timeout = 4000,expected=UnmatchedBracketsException.class)
  public void test08()  throws Throwable  
	  System.out.println(c.evaluate("csc[ <pi>/4 "));
  
  
  @Test(timeout = 4000,expected=ExpressionParserException.class)
  public void test09()  throws Throwable  
	  System.out.println(c.evaluate("<$>"));
  
  
  @Test(timeout = 4000,expected=ExpressionParserException.class)
  public void test10()  throws Throwable  
	  System.out.println(c.evaluate("$100"));
  
  
  @Test(timeout = 4000,expected=ExpressionParserException.class)
  public void test11()  throws Throwable  
	  System.out.println(c.evaluate("abc[100]"));
  
  
  @Test(timeout = 4000,expected=CommandNotFoundException.class)
  public void test12()  throws Throwable  
	 c.parseCommand("lueluelue");
  
  
  @Test(timeout = 4000,expected=CommandNotFoundException.class)
  public void test13()  throws Throwable  
	  assertEquals("-3628800.0",c.evaluate("-fct[ 10 ]"));
  
  
  @Test(timeout = 4000,expected=NullPointerException.class)
  public void test14()  throws Throwable  
	  c.parseCommand("exit");
	  c.parseCommand("help");
	  c.parseCommand("help vars");
	  c.parseCommand("help funcs");
	  c.parseCommand("help cmds"); 
	  c.parseCommand("list"); 
	  c.parseCommand("list cmds"); 
	  c.parseCommand("list vars"); 
	  c.parseCommand("list funcs"); 
	  System.out.println(c.evaluate("<ans>=114"));
  
  
  @Test(timeout = 4000,expected=CommandNotFoundException.class)
  public void test15()  throws Throwable  
	  
	  assertEquals("1013.294133013707",c.evaluate("(<fd>^2 + <wlw>^2)^0.5"));
	  c.expParser.variables[0][0] = "wxy";
	  c.expParser.variables[0][1] = "200";
	  c.expParser.variables[1][0] = "wyc";
	  c.expParser.variables[1][1] = "50";
	  c.expParser.variables[2][0] = "fd";
	  c.expParser.variables[2][1] = "100";
	  assertEquals("2.5",c.evaluate("(<wxy>+<wyc>)/<fd>"));
  
  

以上是关于Calculator JUnit测试用例的主要内容,如果未能解决你的问题,请参考以下文章

在项目中怎么用junit写单元测试用例

结对练习

Eclipse中怎么使用junit测试

如何使用eclipse进行junit测试

软件测试——Calculator测试

如何在Eclipse中自动生成接口和JUnit测试类