测试以Map

时间:2018-09-01 13:17:03

标签: testing state-machine

我有一个状态机,状态和输入的集合相对较少,我想详尽地测试转换。
转换是使用Map<State, Map<Input, State>>进行编码的,代码是这样的:

enum State {
    S1,
    // ...
}

enum Input {
    I1,
    // ...
}

class StateMachine {
    State current;

    Map<State, Map<Input, State>> transitions = {
        S1: {
            I1: S2,
            // ...
        },
        // ...
    };

    State changeState(Input x) {
        if (transitions[current] == null)
            throw Error('Unknows state ${current}');
        if (transitions[current][x] == null)
            throw Error('Unknown transition from state ${current} with input ${x}');

        current = transitions[current][x];
        return current;
    }

    void execute() {
        // ...
    }
}

要测试它,我看到了3种方法:
1)写很多样板代码来检查每个单独的组合
2)自动创建测试:对我来说这似乎是一种更好的方法,但是最终会使用与StateMachine中使用的Map相同的结构。我该怎么办?将地图复制到测试文件中还是从实现文件中导入?后者会使测试文件依赖于实现,这似乎不是一个好主意。
3)测试Map是否相等,是否存在与以前相同的问题:自身或副本是否相等?从本质上讲,这种方法是我对其他2种方法所做的,但似乎不是规范测试

1 个答案:

答案 0 :(得分:1)

也许您想看看以下内容:https://www.itemis.com/en/yakindu/state-machine/documentation/user-guide/sctunit_test-driven_statechart_development_with_sctunit

它显示了如何进行基于模型的测试和状态机的驱动开发,包括生成单元测试代码和测量测试覆盖范围的选项。

相关问题