def test_task_start(self): """ 测试任务下达. """ #1构建模拟任务 data={'TaskNum':100,'Source':'101','Target':'05-01-01','Barcode':'101001001008','State':1,'Priority':1,} task = Task(**data) #task.save() #2创建业务类对象,并调用任务分解函数 taskBiz=TaskBiz() taskBiz.task_decompose(task,None)#① taskBiz.task_start(task)#② self.assertEqual(task.State,Task.STATE_RUNNING) |
from unittest import TestCase import django from selenium import webdriver class FunctionalTest(TestCase): @classmethod def setUpClass(cls): #① super(FunctionalTest, cls).setUpClass() django.setup() cls.browser=webdriver.Chrome() cls.live_server_url = 'http://localhost:8001/task/' @classmethod def tearDownClass(cls): #② cls.browser.quit() def test_task_list(self): #③ self.browser.get(self.live_server_url ) self.browser.maximize_window() self.browser.implicitly_wait(3)#④ #假定网页应该包含“任务列表”的标题 self.assertIn('任务列表',self.browser.title) |
D:\my tfs\IndDemo>python manage.py test Task.functional_test System check identified no issues (0 silenced). DevTools listening on ws://127.0.0.1:64721/devtools/browser/0c65b5e3-1000-4233-a746-30b9142532fa F ====================================================================== FAIL: test_task_list (Task.functional_test.FunctionalTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "D:\my tfs\IndDemo\Task\functional_test.py", line 26, in test_task_list self.assertIn('任务列表',self.browser.title) AssertionError: '任务列表' not found in '' ---------------------------------------------------------------------- Ran 1 test in 6.486s FAILED (failures=1) D:\my tfs\IndDemo> |
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title>任务列表</title> </head> <body> ... |
D:\my tfs\IndDemo>python manage.py test Task.functional_test System check identified no issues (0 silenced). DevTools listening on ws://127.0.0.1:65125/devtools/browser/94e6664d-cda5-41e1-adc7-b9820cb73241 . ---------------------------------------------------------------------- Ran 1 test in 5.677s OK D:\my tfs\IndDemo> |
from unittest import TestCase import django from django.test import LiveServerTestCase from time import sleep from selenium import webdriver from selenium.webdriver.common.keys import Keys from Task.TaskBiz import Task class FunctionalTest(LiveServerTestCase): @classmethod def setUpClass(cls): super(FunctionalTest, cls).setUpClass() django.setup() cls.browser=webdriver.Chrome() #cls.live_server_url = 'http://localhost:8001/task/' #1初始化测试任务1 data={'TaskNum':200,'Source':'101','Target':'05-01-01','Barcode':'101001001008','State':1,'Priority':1,} task = Task(**data) task.save() #2初始化测试任务2 data={'TaskNum':201,'Source':'102','Target':'05-01-02','Barcode':'101001001009','State':1,'Priority':1,} task2 = Task(**data) task2.save() @classmethod def tearDownClass(cls): #② cls.browser.quit() def test_task_list(self): #③ #print(self.live_server_url + '/task/') self.browser.get(self.live_server_url + '/task/' ) self.browser.maximize_window() self.browser.implicitly_wait(3)#④ #假定网页应该包含“任务列表”的标题 self.assertIn("任务列表",self.browser.title) #获取table并断言table row 里是否包含初始化的任务数据 table = self.browser.find_element_by_id('id_task_table') #table = self.browser.find_elements_by_tag_name('table') rows = table.find_elements_by_tag_name('tr') #表标题行 self.assertIn("任务号",rows[0].text) #表第一行数据 self.assertIn('200',rows[1].text) self.assertIn('101001001008',rows[1].text) #表第二行数据 self.assertIn('201',rows[2].text) #对第一个执行任务分解操作 self.browser.find_element_by_id('1-decompose').click() self.browser.implicitly_wait(3) #sleep(3) table = self.browser.find_element_by_id('id_task_table') rows = table.find_elements_by_tag_name('tr') #表第一行数据包含子任务数 10 self.assertIn('10',rows[1].text) self.assertIn('处理成功',rows[1].text) #对第一个执行任务下达操作 self.browser.find_element_by_id('1-start').click() self.browser.implicitly_wait(3) table = self.browser.find_element_by_id('id_task_table') rows = table.find_elements_by_tag_name('tr') #表第一行数据包含子任务数 10 self.assertIn('执行中',rows[1].text) #修改第二个任务 self.browser.find_element_by_id('2-change').click() self.browser.implicitly_wait(3) self.browser.find_element_by_name('source').send_keys('111') self.browser.find_element_by_name('target').send_keys('05-01-11') self.browser.find_element_by_name('target').send_keys(Keys.ENTER) self.browser.implicitly_wait(3) table = self.browser.find_element_by_id('id_task_table') rows = table.find_elements_by_tag_name('tr') self.assertIn('111',rows[2].text) self.assertIn('05-01-11',rows[2].text) |
D:\my tfs\IndDemo>python manage.py test Task.functional_test Creating test database for alias 'default'... System check identified no issues (0 silenced). DevTools listening on ws://127.0.0.1:51015/devtools/browser/82f971f5-83ba-4b55-bc19-ff700e7aedb1 E---------------------------------------- ---------------------------------------- ====================================================================== ERROR: test_task_list (Task.functional_test.FunctionalTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "D:\my tfs\IndDemo\Task\functional_test.py", line 59, in test_task_list self.browser.find_element_by_id('1-decompose').click() File "C:\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id return self.find_element(by=By.ID, value=id_) File "C:\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element 'value': value})['value'] File "C:\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="1-decompose"]"} (Session info: chrome=90.0.4430.72) ---------------------------------------------------------------------- Ran 1 test in 8.513s FAILED (errors=1) Destroying test database for alias 'default'... D:\my tfs\IndDemo> |
... {% for task in tasks %} <tr> <td>{{task.TaskId }}</td> <td>{{task.TaskNum}}</td> <td>{{task.Source}}</td> <td>{{task.Target}}</td> <td>{{task.Barcode}}</td> <td>{{task.get_State_display}}</td> <td>{{task.get_Priority_display}}</td> <td>-</td> <td>-</td> <td>{{task.job_set.count}}</td> <td><a id="{{task.TaskId}}-decompose" href="{{task.TaskId }}/decompose/">分解</a> <a id="{{task.TaskId}}-start" href="{{task.TaskId }}/start/">下达</a> <a id="{{task.TaskId}}-change" href="{{task.TaskId }}/change/">修改</a></td> </tr> {%endfor%} ... |
D:\my tfs\IndDemo>python manage.py test Task.functional_test Creating test database for alias 'default'... System check identified no issues (0 silenced). DevTools listening on ws://127.0.0.1:51636/devtools/browser/89f5a372-fbe3-4ecd-b581-07d813d56c55 . ---------------------------------------------------------------------- Ran 1 test in 6.224s OK Destroying test database for alias 'default'... D:\my tfs\IndDemo> |
... class TaskViewTest(TestCase): """Tests for the application views.""" # Django requires an explicit setup() when running tests in PTVS @classmethod def setUpClass(cls): super(TaskURLTest, cls).setUpClass() django.setup() #1初始化测试任务1 data={'TaskNum':200,'Source':'101','Target':'05-01-01','Barcode':'101001001008','State':1,'Priority':1,} task = Task(**data) task.save() #2初始化测试任务2 data={'TaskNum':201,'Source':'102','Target':'05-01-02','Barcode':'101001001009','State':1,'Priority':1,} task2 = Task(**data) task2.save() def test_task_change(self): data={'source':'111','target':'05-01-11'} #更新第一个task的源和目标值 response=self.client.post('/task/1/change/',data) model = Task.objects.get(pk=1) self.assertEqual(model.Source,'111') self.assertEqual(model.Target,'05-01-11') response=self.client.get('/task/') self.assertIn('111',response.content.decode()) self.assertTemplateUsed(response,'Task/tasks.html') def test_task_decompose(self): response=self.client.get('/task/1/decompose/') model = Task.objects.get(pk=1) self.assertEqual(model.job_set.count(),10) def test_task_decompose(self): self.client.get('/task/1/decompose/') model = Task.objects.get(pk=1) self.assertEqual(model.job_set.count(),10) self.client.get('/task/1/start/') model = Task.objects.get(pk=1) self.assertEqual(model.State,Task.STATE_RUNNING) |
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |