Fakultas Ilmu Komputer UI

Commit 0db83c81 authored by Kevin Albert Simanjuntak's avatar Kevin Albert Simanjuntak
Browse files

menyelesaikan excercise-3

parent 2c9fe62f
Pipeline #21645 passed with stage
in 5 minutes and 29 seconds
stages:
- test
- functional-test
Test:
image: python:3.7
stage: test
before_script:
- apt-get update -qq && apt-get install -y -qq unzip
- apt-get install -qy --fix-missing --no-install-recommends xvfb firefox-esr
- apt-get install -y xvfb
- wget https://github.com/mozilla/geckodriver/releases/download/v0.19.0/geckodriver-v0.19.0-linux64.tar.gz; mkdir geckodriver; tar -xzf geckodriver-v0.19.0-linux64.tar.gz -C geckodriver; export PATH=$PATH:$PWD/geckodriver;
- pip install -r requirements.txt
- pip3 install --upgrade pip
- pip3 install -r requirements.txt
script:
- python3 manage.py test
tags:
- test
functional-test:
image: python:3.7
stage: functional-test
before_script:
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
- echo "deb http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list
- apt-get update -qq && apt-get install -y -qq unzip
- apt-get install -y google-chrome-stable
- apt-get install -y xvfb
- wget https://chromedriver.storage.googleapis.com/77.0.3865.40/chromedriver_linux64.zip
- unzip chromedriver_linux64.zip
- pip install -r requirements.txt
when: on_success
script:
- python3 functional_tests.py
1606835595-practice
url Heroku : https://pmpl-kevin-simanjuntak.herokuapp.com/homepage/
# 160635595-practice
# Heroku Link:
https://pmpl-kevin-simanjuntak.herokuapp.com/homepage/
# Exercise 3 Story
## Testing Isolation
Pada excercise-2 ketika ingin melakukan functional_test maka test tersebut akan dijalankan dengan membuka localhost yang telah dijalankan lalu melakukan testing dengan memasukkan input dan mencocokkan output tersebut dengan output yang seharusnya. Test tersebut sebenarnya berjalan cukup baik, namun masalahnya adalah input dari test tersebut masuk ke database(db.sqlite3). Maka dari itu setiap kali melakukan testing maka data test tersebut harus dihapus dari database. Lain halnya dengan unittest, yang mana test tersebut dilakukan pada database yang terpisah dengan database yang asli.
Dengan menggunakan LiveServerTestCase functional_tests yang telah dibuat dapat dijalankan seperti menjalankan unittest dimana database untuk melakukan test tersebut dipisah dengan database yang asli. Sehingga functional_tests tidak akan merusak database yang asli
## Perbedaan design baru dengan design sebelumnya
Pada design sebelumnya lokasi dari functional_test berada pada lokasi yang sama dengan folder list dan superlist, sedangkan dengan design yang baru functional_test diubah namanya menjadi test lalu dimasukkan kedalam folder functional_tests beserta agar test tersebut akan automatis dipanggil ketika menjalankan perintah python manage.py test
├───functional_tests
│ │ tests.py
│ │ __init__.py
│ │
│ └───__pycache__
│ tests.cpython-37.pyc
│ __init__.cpython-37.pyc
├───lists
│ │ admin.py
│ │ apps.py
│ │ models.py
│ │ tests.py
│ │ urls.py
│ │ views.py
│ │ __init__.py
│ │
│ ├───migrations
│ │ │ 0001_initial.py
│ │ │ 0002_item_text.py
│ │ │ __init__.py
│ │ │
│ │ └───__pycache__
│ │ 0001_initial.cpython-37.pyc
│ │ 0002_item_text.cpython-37.pyc
│ │ __init__.cpython-37.pyc
│ │
│ ├───templates
│ │ home.html
│ │
│ └───__pycache__
│ admin.cpython-37.pyc
Dan juga perbedaan design pada immplementasi functional_test yang sebelumnya menggunakan class unittest.TestCase menjadi menggunakan LiveServerTestCase, dan juga ada perbedaan design untuk implementasi menggunakan time.sleep(5) (belum diimplementasikan secara menyeluruh) dimana setiap melakukan input maka akan ditunggu selama 5 detik agar data inputan tersebut dapat diproses. Diubah menjadi code seperti ini dengan menggunakan max_wait=10
self.wait_for_row_in_list_table('1: Buy peacock feathers')
def wait_for_row_in_list_table(self, row_text):
start_time = time.time()
while True:
try:
table = self.browser.find_element_by_id('id_list_table')
rows = table.find_elements_by_tag_name('tr')
self.assertIn(row_text, [row.text for row in rows])
return
except (AssertionError, WebDriverException) as e:
if time.time() - start_time > MAX_WAIT:
raise e
time.sleep(0.5)
Code tersebut membuat testing berjalan lebih efisien karena program tidak harus menunggu 5 detik untuk setiap inputannya melainkan hanya diberi batas apabila sudah melebihi max_wait maka test tersebut gagal. Jadi apabila input tersebut telah diproses dalam waktu 1 detik maka test tersebut sudah dinyatakan berhasil. Berbeda dengan desain sebelumnya yang menggunakan time.sleep(5) dimana tiap input pasti dijeda 5s.
\ No newline at end of file
No preview for this file type
from django.test import LiveServerTestCase
from selenium.webdriver.firefox.options import Options
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import WebDriverException
import time
MAX_WAIT = 10
class NewVisitorTest(LiveServerTestCase):
def setUp(self):
options = Options()
options.add_argument('--dns-prefetch-disable')
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('disable-gpu')
self.browser = webdriver.Firefox(options=options)
def tearDown(self):
self.browser.quit()
def check_for_row_in_list_table(self, row_text):
table = self.browser.find_element_by_id('id_list_table')
rows = table.find_elements_by_tag_name('tr')
self.assertIn(row_text, [row.text for row in rows])
# She is invited to enter a to-do item straight away
def test_can_start_a_list_and_retrieve_it_later(self):
self.browser.get(self.live_server_url)
# She notices the page title and header mention to-do lists
self.assertIn('To-Do', self.browser.title)
header_text = self.browser.find_element_by_tag_name('h1').text
self.assertIn('To-Do', header_text)
# She is invited to enter a to-do item straight away
inputbox = self.browser.find_element_by_id('id_new_item')
self.assertEqual(
inputbox.get_attribute('placeholder'),
'Enter a to-do item'
)
inputbox.send_keys('Buy peacock feathers')
inputbox.send_keys(Keys.ENTER)
self.wait_for_row_in_list_table('1: Buy peacock feathers')
self.check_comment_based_on_total_list()
inputbox = self.browser.find_element_by_id('id_new_item')
inputbox.send_keys('Use peacock feathers to make a fly')
inputbox.send_keys(Keys.ENTER)
self.wait_for_row_in_list_table('2: Use peacock feathers to make a fly')
self.check_comment_based_on_total_list()
inputbox = self.browser.find_element_by_id('id_new_item')
inputbox.send_keys('Buy peacock')
inputbox.send_keys(Keys.ENTER)
time.sleep(5)
self.check_comment_based_on_total_list()
inputbox = self.browser.find_element_by_id('id_new_item')
inputbox.send_keys('Buy feathers')
inputbox.send_keys(Keys.ENTER)
time.sleep(5)
self.check_comment_based_on_total_list()
inputbox = self.browser.find_element_by_id('id_new_item')
inputbox.send_keys('feathers')
inputbox.send_keys(Keys.ENTER)
time.sleep(5)
self.check_comment_based_on_total_list()
inputbox = self.browser.find_element_by_id('id_new_item')
inputbox.send_keys('Buy')
inputbox.send_keys(Keys.ENTER)
time.sleep(5)
self.check_comment_based_on_total_list()
#comment = self.browser.find_element_by_id('comment').text
#self.assertIn('oakwowakkoaw', comment)
def check_comment_based_on_total_list(self):
table = self.browser.find_element_by_id('id_list_table')
rows = table.find_elements_by_tag_name('tr')
comment = self.browser.find_element_by_id('comment').text
if len(rows)==0:
self.assertIn('yey, waktunya berlibur',comment)
elif len(rows)<5:
self.assertIn('sibuk tapi santai',comment)
else:
self.assertIn('oh tidak',comment)
def wait_for_row_in_list_table(self, row_text):
start_time = time.time()
while True:
try:
table = self.browser.find_element_by_id('id_list_table')
rows = table.find_elements_by_tag_name('tr')
self.assertIn(row_text, [row.text for row in rows])
return
except (AssertionError, WebDriverException) as e:
if time.time() - start_time > MAX_WAIT:
raise e
time.sleep(0.5)
\ No newline at end of file
stages:
- test
Test:
image: python:3.7
stage: test
before_script:
- pip3 install --upgrade pip
- pip3 install -r requirements.txt
script:
- python3 manage.py test
- python3 functional_tests.py
tags:
- test
......@@ -13,10 +13,7 @@
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
{% endfor %}
</table>
<<<<<<< HEAD
<h2 id="comment">{{comment}}</h2>
=======
>>>>>>> c179133ab5bbcea398e11f36d4d4a6a1a65491c9
</body>
</html>
\ No newline at end of file
diff --git a/functional_tests.py b/functional_tests.py
index c9ac63e..a438209 100644
--- a/functional_tests.py
+++ b/functional_tests.py
@@ -35,18 +35,21 @@ class NewVisitorTest(unittest.TestCase):

# When she hits enter, the page updates, and now the page lists
# "1: Buy peacock feathers" as an item in a to-do list table
- inputbox.send_keys(Keys.ENTER) 
- time.sleep(1) 

+ inputbox.send_keys(Keys.ENTER)
+ time.sleep(1)
table = self.browser.find_element_by_id('id_list_table')
rows = table.find_elements_by_tag_name('tr') 
- self.assertTrue(any(row.text == '1: Buy peacock feathers' for row in rows),"New to-do item did not appear in table")
-
- # There is still a text box inviting her to add another item. She
- # enters "Use peacock feathers to make a fly" (Edith is very
- # methodical)
- self.fail('Finish the test!')
-
- # The page updates again, and now shows both items on her list 
+ #self.assertTrue(any(row.text == '1: Buy peacock feathers' for row in rows),f"New to-do item did not appear in table")
+ self.assertIn('1: Buy peacock feathers', [row.text for row in rows])
+ inputbox = self.browser.find_element_by_id('id_new_item')
+ inputbox.send_keys('Use peacock feathers to make a fly')
+ inputbox.send_keys(Keys.ENTER)
+ time.sleep(1)
+ table = self.browser.find_element_by_id('id_list_table')
+ rows = table.find_elements_by_tag_name('tr')
+ self.assertIn('1: Buy peacock feathers', [row.text for row in rows])
+ self.assertIn('2: Use peacock feathers to make a fly',[row.text for row in rows])
+ 
if __name__ == '__main__': 
unittest.main(warnings='ignore')
\ No newline at end of file
diff --git a/lists/__pycache__/tests.cpython-37.pyc b/lists/__pycache__/tests.cpython-37.pyc
index f371935..4b6e9b9 100644
Binary files a/lists/__pycache__/tests.cpython-37.pyc and b/lists/__pycache__/tests.cpython-37.pyc differ
diff --git a/lists/__pycache__/views.cpython-37.pyc b/lists/__pycache__/views.cpython-37.pyc
index 8a440d0..2bd5393 100644
Binary files a/lists/__pycache__/views.cpython-37.pyc and b/lists/__pycache__/views.cpython-37.pyc differ
diff --git a/lists/templates/home.html b/lists/templates/home.html
index 9a2a093..dd544fc 100644
--- a/lists/templates/home.html
+++ b/lists/templates/home.html
@@ -4,8 +4,12 @@
</head>
<body>
​<h1>Your To-Do list</h1>
- ​<input id="id_new_item" placeholder="Enter a to-do item" />
- ​<table id="id_list_table">
- ​</table>
- ​</body>
+ <form method="POST">
+  <input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />
+  {% csrf_token %}
+ </form>
+ <table id="id_list_table">
+  <tr><td>1: {{ new_item_text }}</td></tr>
+  </table>
+ </body>
</html>
\ No newline at end of file
diff --git a/lists/tests.py b/lists/tests.py
index 4e973ff..44473b0 100644
--- a/lists/tests.py
+++ b/lists/tests.py
@@ -20,4 +20,9 @@ class HomePageTest(TestCase):

def test_uses_home_template(self):
response = self.client.get('/homepage/')
+ self.assertTemplateUsed(response, 'home.html')
+
+ def test_can_save_a_POST_request(self):
+ response = self.client.post('/homepage/', data={'item_t
\ No newline at end of file
from django.conf.urls import url, include
from lists import views
import lists.urls as lists
from django.views.generic.base import RedirectView
urlpatterns = [
#url(r'^$', views.home_page, name='home'),
url(r'^homepage/', include(('lists.urls', 'lists'),namespace='lists')),
url(r'^$',RedirectView.as_view(url="/homepage/",permanent="true")),
]
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment