Jupyter notebook で unittest
Jupyter notebook で Webサイトをテストする実験
問題点
JavaScript メインの開発体制なのに
Python を言語として採用するのは正しいか?
JavaScript でやったほうがいい?
code:python
import unittest
import requests
from IPython.display import HTML
from bs4 import BeautifulSoup
class TestHtml(unittest.TestCase):
"""HTML の title タグの正常性確認
"""
def test_title_tag(self):
"""title タグは "UNIX & Linux コマンド・シェルスクリプト リファレンス" であるべき
"""
r = requests.get(URL)
html = r.content.decode()
soup = BeautifulSoup(html, "html.parser")
self.assertEqual("UNIX & Linux コマンド・シェルスクリプト リファレンス", soup.title.string)
https://gyazo.com/6a4648c0fd545df3d6b28c15e2360dbc
ちょっとリファクタリングした
code:python
import unittest
import requests
from IPython.display import HTML
from bs4 import BeautifulSoup
def fetch_site(url):
r = requests.get(url)
html = r.content.decode()
return BeautifulSoup(html, "html.parser")
def fetch_site_title(url):
soup = fetch_site(url)
return soup.title.string
class TestHtml(unittest.TestCase):
"""HTML の title タグの正常性確認
"""
def test_title_tag(self):
"""title タグは "UNIX & Linux コマンド・シェルスクリプト リファレンス" であるべき
"""
self.assertEqual("UNIX & Linux コマンド・シェルスクリプト リファレンス", fetch_site_title(URL))