zoukankan      html  css  js  c++  java
  • 单元测试JUnit4 Ctrl + Shift + T

    单元测试

    public class Calculator {
    public int result = 0;

    public int add(int operandl, int operand2) {
    result = operandl + operand2;
    return result;
    }

    public int subtract(int operandl, int operand2) {
    result = operandl - operand2;
    return result;
    }

    public int multiple(int operandl, int operand2) {
    result = operandl * operand2;
    for (; ; ) {

    }
    }

    public int divide(int operandl, int operand2) {
    result = operandl / 0;
    return result;
    }

    public int getResult() {
    return this.result;
    }

    }



    public class CalculatorTest {
    public static Calculator cal = new Calculator();

    //测试开始前只能被执行一次
    @BeforeClass
    public static void setUpBeforClass() throws Exception {
    System.out.println(" @BeforeClass");

    }

    //测试结束前只调用一次
    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    System.out.println("@AfterClass");
    }

    @Before
    public void setUp() throws Exception {
    System.out.println("测试开始");
    }

    @After
    public void tearDown() throws Exception {
    System.out.println("测试结束");
    }

    @Test
    public void testSubstract() {
    cal.subtract(4, 2);
    assertEquals(2, cal.getResult());
    // fail("Not yet implemented");
    }

    @Test
    public void testAdd() {
    cal.add(2, 2);
    assertEquals(4, cal.getResult());
    // fail("Not yet implemented");
    }

    //忽略执行
    @Ignore
    public void testMultiply() {
    fail("Not yet implemented");

    }

    //测试用例时间不能超过2s,死循环时间超过2s
    @Test(timeout = 2000)
    public void testDivide() {
    for (; ; ) ;
    }

    //执行之后抛出异常才成功
    @Test(expected = ArithmeticException.class)
    public void testDivideByZero() {
    cal.divide(4, 0);
    }

    }

     单元测试WebDriver脚本

    public class dome2 {

    public WebDriver driver;
    String baseUrl="https://www.baidu.com/";
    @Before
    public void setUp(){
    System.setProperty("webdriver.firefox.bin", "C:\Program Files (x86)\Mozilla Firefox\firefox.exe");
    driver = new FirefoxDriver();
    }
    @After
    public void tearDown(){
    driver.quit();
    }
    @Test
    public void test(){
    driver.get(baseUrl);
    }
  • 相关阅读:
    SQL/LINQ/Lamda 写法[转发]
    MVC Linq动态排序
    在webBrowser1.Navigate(url)中设置Cookie的注意点
    Bootstrap系列 -- 15. 下拉选择框select【转发】
    js输出指定n位数的随机数的随机整数方法【转发】
    C#时间戳转换[转发]
    gdb 多线程调试
    linux 的终端字体色和背景色的修改方法(三)
    linux 的终端字体色和背景色的修改方法(二)
    linux 的终端字体色和背景色的修改方法(一)
  • 原文地址:https://www.cnblogs.com/baoyu7yi/p/7145407.html
Copyright © 2011-2022 走看看