Fakultas Ilmu Komputer UI

Commit 8ee696be authored by Rayza Arasj Mahardhika's avatar Rayza Arasj Mahardhika
Browse files

Merge branch 'exercise-3' into 'master'

Exercise 3

See merge request !3
parents 99849ad5 4636ab4e
Pipeline #21418 passed with stage
in 1 minute and 53 seconds
...@@ -2,3 +2,41 @@ ...@@ -2,3 +2,41 @@
PMPL Course - Class A PMPL Course - Class A
URL: http://pmpl-rayza.herokuapp.com URL: http://pmpl-rayza.herokuapp.com
## Exercise - 3 : Test Isolation
### Test Isolation
Test Isolation adalah proses pemecahan sistem / aplikasi menjadi modul-modul kecil yang dapat dilakukan `test` ataupun `evaluasi` secara lebih mudah. Tipe software testing ini biasanya dilakukan jika bug yang ada pada aplikasi sulit untuk ditemukan. Dengan bantuan Test Isolation, testing dapat dilakukan dengan lebih mudah dan tanpa side effect apapun. Hal ini dikarenakan dengan menggunakan Test Isolation, testing akan dilakukan pada environment yang masih bersih dari side effect apapun serta dengan menggunakan database yang bersih pula.
### Implementation
Dalam tutorial-6 yang terdapat pada buku **Test-Driven Development with Python: Obey the Testing Goat: Using Django, Selenium, and JavaScript**, Test Isolation dilakukan dengan cara membuat `functional_test.py` yang sudah dibuat pada tutorial-tutorial sebelumnya menjadi suatu module pada django testing dengan memanfaatkan class `LiveServerTestCase`. Berikut adalah perubahan code yang terjadi dari `functional_test.py` menjadi `functional_tests/tests.py` :
#### Sebelum :
```
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import unittest
class NewVisitorTest(unittest.TestCase):
def setUp(self):
[...]
```
#### Sesudah :
```
from django.test import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import WebDriverException
import time
class NewVisitorTest(LiveServerTestCase):
def setUp(self):
[...]
```
Dengan melakukan perubahan tersebut, To-Do List yang ada pada aplikasi selalu mulai dari kosong pada saat menjalankan test. Berbeda ketika belum menggunakan `LiveServerTestCase` dimana To-Do List yang ada akan terus bertambah saat dijalan dari test ke test berikutnya.
\ No newline at end of file
from django.test import LiveServerTestCase
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import WebDriverException
import time import time
import unittest
class NewVisitorTest(unittest.TestCase): MAX_WAIT = 10
class NewVisitorTest(LiveServerTestCase):
def setUp(self): def setUp(self):
self.browser = webdriver.Firefox() self.browser = webdriver.Firefox()
...@@ -12,15 +15,23 @@ class NewVisitorTest(unittest.TestCase): ...@@ -12,15 +15,23 @@ class NewVisitorTest(unittest.TestCase):
def tearDown(self): def tearDown(self):
self.browser.quit() self.browser.quit()
def check_for_row_in_list_table(self, row_text): 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') table = self.browser.find_element_by_id('id_list_table')
rows = table.find_elements_by_tag_name('tr') rows = table.find_elements_by_tag_name('tr')
self.assertIn(row_text, [row.text for row in rows]) 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)
def test_can_start_a_list_and_retrieve_it_later(self): def test_can_start_a_list_and_retrieve_it_later(self):
# Edith has heard about a cool new online to-do app. She goes # Edith has heard about a cool new online to-do app. She goes
# to check out its homepage # to check out its homepage
self.browser.get('http://localhost:8000') self.browser.get(self.live_server_url)
# She notices the page title and header meantion to-do lists # She notices the page title and header meantion to-do lists
self.assertIn('To-Do', self.browser.title) self.assertIn('To-Do', self.browser.title)
...@@ -42,7 +53,7 @@ class NewVisitorTest(unittest.TestCase): ...@@ -42,7 +53,7 @@ class NewVisitorTest(unittest.TestCase):
# "1: Buy peacock feathers" as an item in a to-do list table # "1: Buy peacock feathers" as an item in a to-do list table
inputbox.send_keys(Keys.ENTER) inputbox.send_keys(Keys.ENTER)
time.sleep(1) time.sleep(1)
self.check_for_row_in_list_table('1: Buy peacock feathers') self.wait_for_row_in_list_table('1: Buy peacock feathers')
# There is still a text box inviting her to add another item. She # There is still a text box inviting her to add another item. She
# enters "Use peacock feathers to make a fly" (Edith is very # enters "Use peacock feathers to make a fly" (Edith is very
...@@ -53,8 +64,8 @@ class NewVisitorTest(unittest.TestCase): ...@@ -53,8 +64,8 @@ class NewVisitorTest(unittest.TestCase):
time.sleep(1) time.sleep(1)
# The page updates again, and now shows both items on her list # The page updates again, and now shows both items on her list
self.check_for_row_in_list_table('1: Buy peacock feathers') self.wait_for_row_in_list_table('1: Buy peacock feathers')
self.check_for_row_in_list_table('2: Use peacock feathers to make a fly') self.wait_for_row_in_list_table('2: Use peacock feathers to make a fly')
# Edith wonders whether the site will remember her list. Then she sees # Edith wonders whether the site will remember her list. Then she sees
# that the site has generated a unique URL for her -- there is some # that the site has generated a unique URL for her -- there is some
...@@ -63,6 +74,3 @@ class NewVisitorTest(unittest.TestCase): ...@@ -63,6 +74,3 @@ class NewVisitorTest(unittest.TestCase):
# She visits that URL - her to-do list is still there. # She visits that URL - her to-do list is still there.
# Satisfied, she goes back to sleep # Satisfied, she goes back to sleep
if __name__ == '__main__':
unittest.main(warnings='ignore')
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