Fakultas Ilmu Komputer UI

Commit 6d7b0045 authored by Farah Alhaniy Efendi's avatar Farah Alhaniy Efendi
Browse files

Merge branch 'exercise6' into 'master'

Exercise6

See merge request !12
parents f240ca48 51a2dcc9
Pipeline #24105 passed with stages
in 8 minutes and 14 seconds
......@@ -13,6 +13,7 @@ Test:
script:
- python3 manage.py test lists
- python3 manage.py muttest lists --modules lists.views
Deployment:
......@@ -34,12 +35,16 @@ FunctionalTest:
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
- pip3 install -r requirements.txt
- 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 google-chrome-stable
- 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;
- pip3 install -r requirements.txt
- python3 manage.py migrate --noinput
- wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
- unzip chromedriver_linux64.zip
- python3 manage.py collectstatic --noinput
when: on_success
script:
......
......@@ -10,7 +10,10 @@ Herokuapp : https://pmpl-farah.herokuapp.com/
- Keterhubungan Chapter 8 dan 7
- Exercise 5 :
- Keterkaitan refactoring dan clean code
- Keuntungan test organization.
- Keuntungan test organization
- Exercise 6 :
- Pembuatan Mutant
- Mutation Testing Tool : Django-mutpy
## Exercise 3
**Proses test isolation**
......@@ -76,3 +79,40 @@ Dengan menerapkan Red Green Refactor, berarti kita juga menerapkan konsep clean
Memudahkan maintenance. Kita dapat dengan mudah menemukan test code yang kita butuhkan karena test sudah teratur dalam kategori-kategori. Selain itu kita dapat memilih test yang ingin dijalankan. Bisa menjalankan semua test, bisa juga menjalankan test spesifik saja.
## Exercise 6
**Pembuatan Mutant**
Pada fungsi views.py berikut adalah code implementasi fitur komentar :
```
def view_list(request, list_id):
list_ = List.objects.get(id=list_id)
items = Item.objects.filter(list=list_)
if len(items) == 0:
comment = 'Yey, waktunya berlibur'
elif len(items) < 5:
comment = 'Sibuk tapi santai'
elif len(items) >= 5:
comment = 'Oh tidak'
return render(request, 'list.html', {'list': list_, 'comment':comment})
```
Setelah mengubah code tersebut beberapa kali, dapat dibuat satu mutant pada line 9 dengan mengubah menjadi :
`elif len(items) > 5:`
Dengan mengubah decision pada line tersebut, test tidak error karena pada test sebelumnya tidak menghandle kondisi dimana `len(items) = 5`. Maka dari itu butuh dibuat test baru yang dapat menghandle kondisi tersebut. Jadi pada test_views.py ditambahkan function test untuk `len(items) = 5`, setelah ditambahkan test tersebut, function tersebut menjadi error, maka mutant sudah berhasil di kill.
**Mutation Testing Tool : Django-mutpy**
Berikut adalah hasil dari mutation testing menggunakan django-mutpy. Tool ini dijalankan pada test_models dan test_views.
```
[0.13787 s] killed by test_passes_correct_list_to_template (lists.tests.test_views.ListViewTest)
[*] Mutation score [9.18592 s]: 100.0%
- all: 38
- killed: 38 (100.0%)
- survived: 0 (0.0%)
- incompetent: 0 (0.0%)
- timeout: 0 (0.0%)
Destroying test database for alias 'default'...
```
Dari hasil tersebut dapat kita lihat bahwa semua mutant sudah berhasil di kill oleh test yang ada.
\ No newline at end of file
from selenium.common.exceptions import WebDriverException
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.chrome.options import Options
import time
import environ
......@@ -10,10 +10,14 @@ MAX_WAIT = 10
class FunctionalTest(StaticLiveServerTestCase):
def setUp(self):
options = Options()
options.headless = True
self.browser = webdriver.Firefox(options=options)
self.browser.implicitly_wait(5)
chrome_options = Options()
chrome_options.add_argument('--dns-prefetch-disable')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('disable-gpu')
self.browser = webdriver.Chrome('./chromedriver', chrome_options=chrome_options)
self.browser.implicitly_wait(10)
def tearDown(self):
self.browser.quit()
......
from .base import FunctionalTest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.chrome.options import Options
......@@ -70,11 +70,14 @@ class NewVisitorTest(FunctionalTest):
## We use a new browser session to make sure that no information
## of Edith's is coming through from cookies etc
self.browser.quit()
options = Options()
options.headless = True
self.browser = webdriver.Firefox(options=options)
self.browser.implicitly_wait(5)
chrome_options = Options()
chrome_options.add_argument('--dns-prefetch-disable')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('disable-gpu')
self.browser = webdriver.Chrome('./chromedriver', chrome_options=chrome_options)
self.browser.implicitly_wait(10)
# Francis visits the home page. There is no sign of Edith's
# list
......
......@@ -119,7 +119,15 @@ class HomePageTest(TestCase):
response = self.client.get(f'/lists/{list_.id}/')
self.assertIn('Sibuk tapi santai', response.content.decode())
def test_comment_todolist_more_than_equal_5(self):
def test_comment_todolist_equal_5(self):
list_ = List.objects.create()
for i in range(5):
Item.objects.create(text='itemey ' + str(i), list=list_)
response = self.client.get(f'/lists/{list_.id}/')
self.assertIn('Oh tidak', response.content.decode())
def test_comment_todolist_more_than_5(self):
list_ = List.objects.create()
for i in range(8):
Item.objects.create(text='itemey ' + str(i), list=list_)
......
......@@ -7,4 +7,5 @@ psycopg2==2.7.3.1
selenium==3.141.0
sqlparse==0.3.0
urllib3==1.25.3
whitenoise==4.1.3
\ No newline at end of file
whitenoise==4.1.3
django-mutpy==0.1.2
\ No newline at end of file
......@@ -39,7 +39,8 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'lists'
'lists',
'django_mutpy',
]
MIDDLEWARE = [
......
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