在线OJ系统管理员功能模块测试用例的设计及测试
Posted Ischanged
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在线OJ系统管理员功能模块测试用例的设计及测试相关的知识,希望对你有一定的参考价值。
测试用例的编写
selenium和unittest的测试代码
新增题目,删除用户,删除题目
from selenium import webdriver
import unittest
import time
import os
from selenium.webdriver.common.by import By
class AddProblems(unittest.TestCase):
# 测试固件
def setUp(self):
self.driver=webdriver.Chrome()
#每测试一个功能之前就先登录才能够测其他的功能页
self.driver.get("http://175.178.218.103:8080/onlineOJ/login.html")
self.driver.find_element(By.ID,"username").send_keys("李敏敏")
self.driver.find_element(By.ID,"password").send_keys("123")
self.driver.find_element(By.ID,"submit").click()
time.sleep(2)
#self.url="http://175.178.218.103:8080/onlineOJ/function.html"
self.driver.get("http://175.178.218.103:8080/onlineOJ/function.html")
self.driver.maximize_window()
time.sleep(2)
def tearDown(self):
self.driver.quit()
#
def test_addProblems(self):
driver=self.driver
# url=self.url
# driver.get(url)
time.sleep(1)
driver.find_element(By.LINK_TEXT,"新增题目").click()
driver.implicitly_wait(3)
#断言
#self.assertIsNot("新增题目页",)
try:
self.assertEqual("新增土木页",driver.title,msg="预期结果与实际结果不相同")
except:
self.saveScreenAsPhoto(driver,"bug.png")
driver.find_element(By.ID,"title").send_keys("两数之和")
driver.find_element(By.ID,"level").send_keys("简单")
driver.find_element(By.ID,"description").send_keys("给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。\\n" +
"\\n" +
"你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。\\n" +
"\\n" +
"你可以按任意顺序返回答案。\\n" +
"\\n" +
" \\n" +
"\\n" +
"示例 1:\\n" +
"\\n" +
"输入:nums = [2,7,11,15], target = 9\\n" +
"输出:[0,1]\\n" +
"解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。\\n" +
"示例 2:\\n" +
"\\n" +
"输入:nums = [3,2,4], target = 6\\n" +
"输出:[1,2]\\n" +
"示例 3:\\n" +
"\\n" +
"输入:nums = [3,3], target = 6\\n" +
"输出:[0,1]\\n" +
" \\n" +
"\\n" +
"提示:\\n" +
"\\n" +
"2 <= nums.length <= 104\\n" +
"-109 <= nums[i] <= 109\\n" +
"-109 <= target <= 109\\n" +
"只会存在一个有效答案\\n" +
"进阶:你可以想出一个时间复杂度小于 O(n2) 的算法吗?\\n" +
"\\n" +
"来源:力扣(LeetCode)\\n" +
"链接:https://leetcode-cn.com/problems/two-sum\\n" +
"著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。")
driver.find_element(By.ID,"TestCode").send_keys("public static void main(String[] args) \\n" +
" Solution solution = new Solution();\\n" +
" // testcase1\\n" +
" int[] nums = 2,7,11,15;\\n" +
" int target = 9;\\n" +
" int[] result = solution.twoSum(nums, target);\\n" +
" if (result.length == 2 && result[0] == 0 && result[1] == 1) \\n" +
" System.out.println(\\"testcase1 OK\\");\\n" +
" else \\n" +
" System.out.println(\\"testcase1 failed!\\");\\n" +
" \\n" +
"\\n" +
" // testcase2\\n" +
" int[] nums2 = 3,2,4;\\n" +
" int target2 = 6;\\n" +
" int[] result2 = solution.twoSum(nums2, target2);\\n" +
" if (result2.length == 2 && result[0] == 1 && result[1] == 2) \\n" +
" System.out.println(\\"testcase2 OK\\");\\n" +
" else \\n" +
" System.out.println(\\"testcase2 failed!\\");\\n" +
" \\n" +
" \\n")
driver.find_element(By.ID,"codeEditor").send_keys("class Solution \\n" +
" public int[] twoSum(int[] nums, int target) \\n" +
"\\n" +
" \\n" +
"")
driver.find_element(By.ID,"submitButton").click()
#删除题目
def test_deleteProblems(self):
driver=self.driver
driver.find_element(By.LINK_TEXT, "删除题目").click()
driver.implicitly_wait(1)
problems=driver.find_elements(By.ID,"submit")
for problem in problems:
if problem.get_attribute('value') =='1':
problem.click()
time.sleep(2)#这一句不写就不能删除,来不及删除
#删除用户功能
unittest.skip("skipping")
def test_deleteUser(self):
driver=self.driver
driver.find_element(By.LINK_TEXT,"删除用户").click()
driver.implicitly_wait(1)
driver.find_element(By.ID,"username").send_keys("狗狗")
driver.find_element(By.ID,"submit").click()
driver.implicitly_wait(1)
def saveScreenAsPhoto(self, driver, file_name):
if not os.path.exists("./image"):
os.makedirs("./image")
now = time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))
driver.get_screenshot_as_file("./image/" + now + "-" + file_name)
time.sleep(3)
if __name__ == "__main__":
unittest.main()
查看题目
from selenium import webdriver
import unittest
import time
import os
from selenium.webdriver.common.by import By
class LookProb (unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
# 每测试一个功能之前就先登录才能够测其他的功能页
self.driver.get("http://175.178.218.103:8080/onlineOJ/login.html")
self.driver.find_element(By.ID, "username").send_keys("李敏敏")
self.driver.find_element(By.ID, "password").send_keys("123")
self.driver.find_element(By.ID, "submit").click()
time.sleep(2)
# self.url="http://175.178.218.103:8080/onlineOJ/function.html"
self.driver.get("http://175.178.218.103:8080/onlineOJ/function.html")
self.driver.maximize_window()
time.sleep(2)
def tearDown(self):
self.driver.quit()
def test_look(self):
driver = self.driver
driver.find_element(By.LINK_TEXT, "查看题目").click()
time.sleep(3)
创建测试套件,HTML报告的生成
import HTMLTestRunner
import os
import time
import unittest
import sys
import addProblems
import lookProblems
def createSuit():
suit=unittest.TestSuite()
suit.addTest(unittest.makeSuite(addProblems.AddProblems))
suit.addTest(unittest.makeSuite(lookProblems.LookProb))
#suit.addTest(addProblems.AddProblems("test_addProblems"))
return suit
if __name__ == "__main__":
curpath=sys.path[0]#当前文件所在的路径
print(sys.path)
print("当前路径"+sys.path[0])
if not os.path.exists(curpath+'/resultport'):
os.makedirs(curpath+'/resultport')#当前文件所在的路径不存在这个文件
now=time.strftime("%Y-%m-%d-%H %M %S",time.localtime(time.time()))
print(now)
#
filename = curpath + '/resultport' + now + 'resultreport.html'
with open(filename, 'wb') as fp:
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"测试报告",
description=u"用例执行情况", verbosity=2)
suit=createSuit()
runner.run(suit)
测试功能存在的bug
1.添加题目时,添加的题目内容不全时也能够添加题目,没有进行限制和提示。
2.能够添加一道和之前题目列表里面一模一样的题目。
3.点击每一道题目后面对应的删除按钮,应该出现确认弹窗,之后点击确认弹窗之后删除题目,但是实际的情况是没有弹窗直接删除。
以上是关于在线OJ系统管理员功能模块测试用例的设计及测试的主要内容,如果未能解决你的问题,请参考以下文章