第三次作业

第一题

采用JUnit软件测试框架进行测试程序编程,实现对下面java程序进行单元测试,找出其中缺陷。然后修改缺陷,直到通过单元测试,给出测试程序脚本和运行结果界面。

1
2
3
4
5
6
7
8
9
10
11
12
public class getMax {
public int get_max(int x, int y, int z) {
int max;
if (x >= y)
max = x;
else
max = y;
if (z >= x)
max = z;
return max;
}
}

答:

测试程序脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import junit.framework.TestCase;

public class getMaxTest extends TestCase {
@Test
public void test(){
getMax Max=new getMax();
int max= Max.get_max(7,9,8);
Assert.assertEquals(9,max);
}

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}
}

以下是运行结果,发现代码有缺陷:z如果比x大,但比y小,程序会输出z,而实际情况是y最大。

p9zYRRP.png

修改缺陷,以下是修改后的源程序:

1
2
3
4
5
6
7
8
9
10
11
12
public class getMax {
public int get_max(int x, int y, int z) {
int max;
if (x >= y)
max = x;
else
max = y;
if (z >= max)
max = z;
return max;
}
}

其实就是将其中一个x改成max

以下是单元测试通过的界面:

p9zYoZQ.png

缺陷修改成功。

第二题

采用Postman接口测试软件对百度百科https://baike.baidu.com/搜索引擎进行接口测试。如对词条“软件工程”返回页面内容进行测试验证,给出请求参数设置、Tests脚本、Body响应结果、Test Results结果说明及运行界面。


以下是笔者的分析过程:

​ 在百度百科页面搜索软件工程,得到的网址如下:

https://baike.baidu.com/item/软件工程/25279?fromModule=lemma_search-box

如果是常规的get请求(大多数项目开发者会这样设置),在url里面应该有?word=软件工程的字符串(不一定是word这个单词,举这个例子表示形如这个url),但是该链接并不如此。

​ 于是猜测是post请求,F12打开开发者工具对输入框进行分析,发现它在form表单里,初步断定是post请求,但是这个表单不是向当前网址提交的,表单的action属性是/seach/word,所以请求的url应该是其对应的绝对路径(还得拐个弯🥺),为https://baike.baidu.com/search/word

p9zUnPA.png

p9zalWR.png

​ 还要注意到表单的method是GET方法,于是我大胆的猜测get方法也可以,经过postman测试,用get方法向https://baike.baidu.com/search也可以达到预期目标。

p9za3S1.png


答:

请求参数设置

URL:https://baike.baidu.com/search/word

Method: post

Body:

​ 键:word 值:软件工程

或者

URL:https://baike.baidu.com/search

Method:GET

参数:

​ 键:word 值:软件工程

Tests脚本

1
2
3
4
5
6
7
8
9
10
11
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});

pm.test("Response body is not empty", function () {
pm.response.to.have.body();
});

pm.test("The response contains the word '软件工程'", function () {
pm.expect(pm.response.text()).to.include("软件工程");
});

Body响应结果

把你操作界面的body复制下来就可以啦~内容有很多(毕竟是百度的程序员😎)

Test Results结果

p9za5pn.png

可以看到Status code is 200,Response body is not empty,The response contains the word ‘软件工程’,三次测试全部通过。

运行界面

p9zaIlq.png

但是为了图方便感觉用get直接向https://baike.baidu.com/item/软件工程发请求也得行(这样子就少了很多分析啦~)