项目:妍妍的相册程序——测试

    技术2022-07-10  99

    妍妍的相册程序

    一、单元测试

    在最开始的dao层开发时,对数据库的基本增删改查功能基于junit框架进行单元测试 这样做让我可以更早的发现BUG,也更容易修改。

    1.在maven项目的pom文件添加junit依赖

    <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <!--<scope>test</scope>--> </dependency> </dependencies>

    2.相关插件下载:见这篇博客 3.测试ImageDao层的所有方法(用快捷键生成测试类,然后补充)

    //ImageDaoTest.java package dao; import org.junit.Test; import java.util.ArrayList; import java.util.List; import static org.junit.Assert.*; public class ImageDaoTest { @Test public void insert() { ImageDao imageDao = new ImageDao(); //准备数据 Image image = new Image(); image.setImageName("2.jpg"); image.setSize(1024); image.setUploadTime("20200701"); image.setContentType("image/jpeg"); image.setPath("./image/d48616ccd25273c8d94e107808323570 "); image.setMd5("d48616ccd25273c8d94e107808323570"); boolean flag = imageDao.insert(image); System.out.println("result="+flag); } @Test public void selectAll() { ImageDao imageDao = new ImageDao(); List<Image> lists = imageDao.selectAll(); System.out.println("size ="+ lists.size()); System.out.println(); for (Image image: lists){ System.out.println("name = "+image.getImageName()); } } @Test public void selectOne() { ImageDao imageDao = new ImageDao(); Image image = imageDao.selectOne(60); System.out.println(image); } @Test public void delete() { ImageDao imageDao = new ImageDao(); boolean flag = imageDao.delete(66); } @Test public void selectByMd5() { ImageDao imageDao = new ImageDao(); String md5 = "5e655fddb732603f7501354697de9673"; Image image = imageDao.selectByMd5(md5); System.out.println("name = "+image.getImageName()); } }

    测试结果:

    二、接口测试

    在验证HTTP协议的GET/POST/DELETE方法时借助了Postman测试工具的断言功能来测试HTTP的api接口

    二、功能测试

    基于测试用例完成

    三、自动化测试

    对上传、删除、查看图片功能编写了脚本

    1.用unittest框架编写脚本

    #test0701.py from selenium import webdriver import unittest import time class imageTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.driver.get("http://182.92.83.207:8080/java_image_server/index.html") self.driver.maximize_window() time.sleep(3) def tearDown(self): self.driver.quit() #上传 @unittest.skip("skipping") def test_upload(self): self.driver.find_element_by_name("upload").send_keys("/Users/yanyan/Desktop/证件照/11.jpg") time.sleep(3) self.driver.find_element_by_xpath("//*[@id='blog-collapse']/form/div/div/input").click() time.sleep(3) #删除 @unittest.skip("skipping") def test_delete(self): self.driver.find_element_by_xpath("/html/body/div[1]/figure/div/div/button").click() time.sleep(3) alert = self.driver.switch_to.alert alert.accept() time.sleep(3) #查看图片 def test_check(self): self.driver.find_element_by_xpath("/html/body/div[1]/figure/div/div/img").click() time.sleep(3) self.driver.find_element_by_xpath("/html/body/div[2]/div[2]/a").click() time.sleep(3) if __name__=="__main__": unittest.main()

    脚本运行通过

    2.普通脚本

    #0701.py from selenium import webdriver import time driver = webdriver.Firefox() driver.get("http://182.92.83.207:8080/java_image_server/index.html") driver.maximize_window() #上传 driver.find_element_by_name("upload").send_keys("/Users/yanyan/Desktop/证件照/11.jpg") time.sleep(3) driver.find_element_by_xpath("//*[@id='blog-collapse']/form/div/div/input").click() time.sleep(3) #删除 driver.find_element_by_xpath("/html/body/div[1]/figure/div/div/button").click() time.sleep(3) alert = driver.switch_to.alert alert.accept() time.sleep(3) driver.quit()

    四、简单的并发性测试

    根据分析,将线程数设置为100,模拟多个用户同时访问网站 测试结果如下,并发测试通过

    Processed: 0.018, SQL: 9