开发者社区> 问答> 正文

在JGiven的测试场景中实施测试用例

提供了一个包含许多行的文件,该文件由(接受)系统加载到数据库中。一个基于JGiven的简单测试方案将检查文件的行数是否与相应的表行匹配。Java 1.8 中的样机实现的maven测试执行,提供了以下内容(第一个输出):

Test Class: com.testcomp.LoadingTest Check Loading Process Given that the parameters provided are for an input file ( BRANCH )for a specific date ( 20190105 )was provided When the number of file records is calculated And the loading is complete And the number of table rows loaded is calculated Then the no of file records (200) must match the no of table rows (200) 这是一个有效的选项,但是不提供要加载的文件。因此,我们有两个测试用例,maven测试应提供类似(第二个输出)的内容:

Test Class: com.testcomp.LoadingTest1 Check Loading Process Given that the parameters provided are for an input file ( BRANCH )for a specific date ( 20190105 ) And the file exists When the number of file records is calculated And the loading is complete And the number of table rows loaded is calculated Then the no of file records (200) must match the no of table rows (200)

Test Class: com.testcomp.LoadingTest2 Check Loading Process Given that the parameters provided are for an input file ( BRANCH )for a specific date ( 20190105 ) And the file does NOT exist Then we check nothing 我们如何结合两个测试用例,以便根据文件的存在情况在两种情况下都通过“通过”测试,知道是否提供了文件? 或 有一种方法可以创建类似于JGiven本身的HTML报告的内容 (第三输出):

Test Class: com.testcomp.LoadingTest3 Check Loading Process Given that the parameters provided are for an input file ( BRANCH )for a specific date ( 20190105 ) And the scenario has 2 cases from which only one may be true When checking the file's existence is done And for case 1 file existence is TRUE And for case 1 the number of file records is calculated And for case 1 the loading is complete And for case 1 the number of table rows loaded is calculated And for case 2 file existence is FALSE Then for case 1 the no of file records (200) must match the no of table rows (200) And for case 2 we check nothing 第一个输出的实现如下:

LoadingTest类

public class LoadingTest extends ScenarioTest<GivenWeHaveFile2Load, WhenFileAndDatabaseAreChecked, ThenCheckIfNoOfFileLinesMatchesNoOfTableRows> {

@ScenarioStage
WhenFileAndDatabaseAreChecked loadingFinishedState;

@ScenarioStage
WhenFileAndDatabaseAreChecked databaseState;

@Test
public void Check_Loading_Process() {
    given().that_an_input_file_for_a_specific_date_was_provided("BRANCH", "20190105");
    when().the_number_of_file_records_is_calculated();
    loadingFinishedState
            .and().the_loading_is_complete();
    databaseState
            .and().the_number_of_table_rows_loaded_is_calculated();
    then().the_no_of_file_records_must_match_the_no_of_table_rows();
}

} GivenWeHaveFile2Load类

public class GivenWeHaveFile2Load extends Stage {

@ProvidedScenarioState
String properFileName;

@As( "that the parameters provided are for an input file ($) for a specific date ($) was provided" )
public GivenWeHaveFile2Load that_an_input_file_for_a_specific_date_was_provided(String filenamePrefix, String dateStringYYYYMMDD) {
    properFileName = filenamePrefix + "_" + dateStringYYYYMMDD + ".txt";
    return self();
}

} WhenFileAndDatabaseAreChecked类

public class WhenFileAndDatabaseAreChecked extends Stage {

@ExpectedScenarioState
String properFileName;

@ProvidedScenarioState
int noOfFileRecords;

@ProvidedScenarioState
int noOfTableRows;

// @ExtendedDescription("after we check the number of file lines") // does NOT seem to work..
public WhenFileAndDatabaseAreChecked the_number_of_file_records_is_calculated() {
    // we'll use properFileName to get noOfFileRecords in the actual implementation
    noOfFileRecords = 200;
    return self();
}

public WhenFileAndDatabaseAreChecked the_loading_is_complete() {
    return self();
}

public WhenFileAndDatabaseAreChecked the_number_of_table_rows_loaded_is_calculated() {
    noOfTableRows = 200;
    return self();
}

} ThenCheckIfNoOfFileLinesMatchesNoOfTableRows类

public class ThenCheckIfNoOfFileLinesMatchesNoOfTableRows extends Stage {

@ExpectedScenarioState
int noOfFileRecords;

@ExpectedScenarioState
int noOfTableRows;

@ScenarioState
CurrentStep currentStep;

public ThenCheckIfNoOfFileLinesMatchesNoOfTableRows the_no_of_file_records_must_match_the_no_of_table_rows () {
    currentStep.setName("the no of file records (" + noOfFileRecords + ") must match the no of table rows (" + noOfTableRows + ")");
    assert(noOfFileRecords == noOfTableRows);
    return self();
}

}

展开
收起
被纵养的懒猫 2019-10-08 17:12:06 563 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
移动互联网测试到质量的转变 立即下载
给ITer的技术实战进阶课-阿里CIO学院独家教材(四) 立即下载
F2etest — 多浏览器兼容性测试整体解决方案 立即下载