cucumber java test

Cucumber 详细介绍
Cucumber 是一个能够理解用普通语言 描述的测试用例的支持行为驱动开发(BDD)的自动化测试工具,用Ruby编写,支持Java和.Net等多种开发语言。

对于自动化测试首先需要保证其功能是对客户有价值的和正确可用的。而这一切的基础就是用例要能测试客户的需求,期望,最好能让客户参与到测试用例的开发过程中来或让客户评审测试用例,因此出现了ATDD、BDD等各种理论方法来支撑这一行为。现有很多自动化测试工具可支持ATDD、BDD等,比如Cucumber1、RobotFramework2、SpecFlow3、JBehave4、Fitness5、Concordion6等。

详细參考:http://www.cnblogs.com/vincent2010/p/4943031.html

 

cucumber用例编写
创建feature文件

Feature: 在百度中搜索

@TestngScenario
Scenario: 搜索testng
Given 打开百度,验证title
When 输入 "testng"
Then 点击搜索按钮
Then 清除搜索框

feature文件支持的语言输出内容:

| feature | "功能" |
| background | "背景" |
| scenario | "场景", "剧本" |
| scenario outline | "场景大纲", "剧本大纲" |
| examples | "例子" |
| given | "* ", "假如", "假设", "假定" |
| when | "* ", "当" |
| then | "* ", "那么" |
| and | "* ", "而且", "并且", "同时" |
| but | "* ", "但是" |
| given (code) | "假如", "假设", "假定" |
| when (code) | "当" |
| then (code) | "那么" |
| and (code) | "而且", "并且", "同时" |
| but (code) | "但是" |

 

用例执行步骤编写

public class Search extends CucumbeRunner{

    @Given("^打开百度,验证title$")
    public void setp1() throws Throwable {
        assertEquals(BrowserDriver.dr.getTitle(), "百度一下,你就知道");
    }

    @When("^输入 \"(.*?)\"$")
    public void setp2(String text) throws Throwable {
        driver.type("//*[@id='kw']", text);
    }

    @Then("^点击搜索按钮$")
    public void setp3() throws Throwable {
        driver.click("//*[@id='su']");
    }

    @Then("^清除搜索框$")
    public void setp4() throws Throwable {
        String[] ss = {"//*[@id='kw']"};
        driver.clearText(ss);
    }
}

 

启动器

@CucumberOptions(strict = true, monochrome = true, features = "src/test/resources/features", 
glue = "testcase", 
plugin = {"pretty", "html:target/cucumber-html-report;","json:target/cucumber.json" }, 
tags = { "@TestngScenario" })
public class CucumbeRunner extends AbstractTestNGCucumberTests {
    public static BrowserDriver driver = null;

    @BeforeSuite(alwaysRun = true)
    public void setUp() throws Exception {
        driver = new BrowserDriver("http://www.baidu.com");
    }

    @AfterSuite(alwaysRun = true)
    public void quit() throws IOException, InterruptedException {
        driver.close();
    }

}

 

其他參考:

理解自动化测试框架设计与CBT:https://zhuanlan.zhihu.com/p/31250458

前端测试框架 Jest:https://zhuanlan.zhihu.com/p/28247899

The Difference Between TDD and BDD:http://joshldavis.com/2013/05/27/difference-between-tdd-and-bdd/

 

TDD与BDD差别:

TDD:

var assert = require('assert'),
    factorial = require('../index');

suite('Test', function (){
    setup(function (){
        // Create any objects that we might need
    });

    suite('#factorial()', function (){
        test('equals 1 for sets of zero length', function (){
            assert.equal(1, factorial(0));
        });

        test('equals 1 for sets of length one', function (){
            assert.equal(1, factorial(1));
        });

        test('equals 2 for sets of length two', function (){
            assert.equal(2, factorial(2));
        });

        test('equals 6 for sets of length three', function (){
            assert.equal(6, factorial(3));
        });
    });
});
module.exports = function (n) {
    if (n < 0) return NaN;
    if (n === 0) return 1;

    return n * factorial(n - 1);
};

 

BDD:

var assert = require('assert'),
    factorial = require('../index');

describe('Test', function (){
    before(function(){
        // Stuff to do before the tests, like imports, what not
    });

    describe('#factorial()', function (){
        it('should return 1 when given 0', function (){
            factorial(0).should.equal(1);
        });

        it('should return 1 when given 1', function (){
            factorial(1).should.equal(1);
        });

        it('should return 2 when given 2', function (){
            factorial(2).should.equal(2);
        });

        it('should return 6 when given 3', function (){
            factorial(3).should.equal(6);
        });
    });

    after(function () {
        // Anything after the tests have finished
    });
});

 

posted @ 2017-12-01 10:28  念槐聚  阅读(624)  评论(0)    收藏  举报