本帖最后由 测试积点老人 于 2018-12-28 13:50 编辑
1、响应断言选项
2、选项对应的检查点类型:Assession.Type
如图,界面上选择了检查点类型,保存后的配置文件中保存的对应编码为:
- //断言类型对应的编码
- private final static int MATCH = 1 << 0;
- final static int CONTAINS = 1 << 1;
- private final static int NOT = 1 << 2;
- private final static int EQUALS = 1 << 3;
- private final static int SUBSTRING = 1 << 4;
- 1:表示匹配
- 2:代表contains
- 8:代表equals
- 16:代表substring
- 6:不包含
- 5:不匹配
- 12:不相等
- 20:非substring
复制代码 3、断言源码片段讲解此类位于: org.apache.jmeter.assertions.ResponseAssertion.java - //断言类型的判断就是根据配置文件中保存的编码
- boolean contains = isContainsType(); //判断是否是contains断言
- boolean equals = isEqualsType(); //判断是否是equals断言
- boolean substring = isSubstringType();//判断是否是Substring断言
- boolean matches = isMatchType();//判断是否是Match(匹配)断言
- boolean debugEnabled = log.isDebugEnabled();
- if (debugEnabled){
- log.debug("Type:" + (contains?"Contains":"Match") + (notTest? "(not)": ""));
- }
-
- try {
- // Get the Matcher for this thread
- Perl5Matcher localMatcher = JMeterUtils.getMatcher();
- PropertyIterator iter = getTestStrings().iterator();
- while (iter.hasNext()) {
- String stringPattern = iter.next().getStringValue();
- Pattern pattern = null;
- if (contains || matches) {
- pattern = JMeterUtils.getPatternCache().getPattern(stringPattern, Perl5Compiler.READ_ONLY_MASK);
- }
- boolean found;
- if (contains) { //可以看到,如果断言为contains,则直接调的为String中的contains方法并且支持正则表达式
- found = localMatcher.contains(toCheck, pattern);
- } else if (equals) {//如果断言为equals,则直接调的String的equals方法,不支持正则
- found = toCheck.equals(stringPattern);
- } else if (substring) {
- //如果断言为Substring,则调用的是indexOf。如果从源码看,contains使用的也是indexOf做判断的。indexOf会做一定的匹配,然后把匹配的第一个字符的位置返回,返回的是int类型,如果没找到,那么返回-1。之所以有这两个方法存在,个人感觉应该是为了匹配jdk低版本。indexOf从java1.0就有,而contains从java5.0才有。
- found = toCheck.indexOf(stringPattern) != -1;
- } else {//其他类型的判断和contains就没有区别了,匹配走的也是这个分支
- found = localMatcher.matches(toCheck, pattern);
- }
- pass = notTest ? !found : found;
- if (!pass) {
- if (debugEnabled){log.debug("Failed: "+stringPattern);}
- result.setFailure(true);
- result.setFailureMessage(getFailText(stringPattern,toCheck));
- break;
- }
- if (debugEnabled){log.debug("Passed: "+stringPattern);}
- }
- } catch (MalformedCachePatternException e) {
- result.setError(true);
- result.setFailure(false);
- result.setFailureMessage("Bad test configuration " + e);
- }
- return result;
复制代码 4、日志输出源码片段
- switch (getTestType()) {
- case CONTAINS:
- case SUBSTRING: //可以看出Contains和substring同样都是contain
- sb.append(" expected to contain ");
- break;
- case NOT | CONTAINS:
- case NOT | SUBSTRING:
- sb.append(" expected not to contain ");
- break;
- case MATCH:
- sb.append(" expected to match ");
- break;
- case NOT | MATCH:
- sb.append(" expected not to match ");
- break;
- case EQUALS:
- sb.append(" expected to equal ");
- break;
- case NOT | EQUALS:
- sb.append(" expected not to equal ");
- break;
- default:// should never happen...
- sb.append(" expected something using ");
- }
复制代码
|