作业笔记12_unit test

  1. 编写测试,检查任意一个百科词条页面是否有摘要内容
  2. 编写测试,检查任意一个百科词条页面是否有目录内容

代码部分

from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get("http://baike.baidu.com/view/62095.htm")
try:
    assert "目录" in driver.find_element_by_class_name("block-title").text
    print("该词条包含目录")
except:
    print("该词条没有目录")

try:
    assert driver.find_element_by_xpath("//div[@class='lemma-summary']") is not None
    print("该词条包含摘要")
except:
    print("该词条没有摘要")

driver.close()

运行结果

尝试将"目录"改为"目lu",结果变成:


呀,其实并没有搞懂,只是勉为其难的得到了想要的结果。本来想先用unittest做一下的,折腾不出来。

运行课程代码倒是可以运行。修改了urlopen和unquote的载入,和BeautifulSoup的解析参数。如下:

from urllib.request import urlopen
from urllib.parse import unquote
import random
import re
from bs4 import BeautifulSoup
import unittest

class TestWikipedia(unittest.TestCase):
    
    bsObj = None
    url = None

    def test_PageProperties(self):
        global bsObj
        global url

        url = "http://en.wikipedia.org/wiki/Monty_Python"
        #Test the first 100 pages we encounter
        for i in range(1, 100):
            bsObj = BeautifulSoup(urlopen(url), "lxml")
            titles = self.titleMatchesURL()
            self.assertEqual(titles[0], titles[1])
            self.assertTrue(self.contentExists())
            url = self.getNextLink()
        print("Done!")

    def titleMatchesURL(self):
        global bsObj
        global url
        pageTitle = bsObj.find("h1").get_text()
        urlTitle = url[(url.index("/wiki/")+6):]
        urlTitle = urlTitle.replace("_", " ")
        urlTitle = unquote(urlTitle)
        return [pageTitle.lower(), urlTitle.lower()]

    def contentExists(self):
        global bsObj
        content = bsObj.find("div",{"id":"mw-content-text"})
        if content is not None:
            return True
        return False

    def getNextLink(self):
        global bsObj
        links = bsObj.find("div", {"id":"bodyContent"}).findAll("a", href=re.compile("^(/wiki/)((?!:).)*$"))
        link = links[random.randint(0, len(links)-1)].attrs['href']
        print("Next link is: "+link)
        return "http://en.wikipedia.org"+link

unittest.main()

结果输出:

作业笔记12_unit test_第1张图片

疑问:

#使用selenium测试wiki
from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get("http://en.wikipedia.org/wiki/Monty_Python")
print(driver.title)
assert "Monty Python" in driver.title
print("Monty Python was not in the title")

这段代码运行的结果是:

Monty Python - Wikipedia
Monty Python was not in the title

根本就在title里好吧。所以assert如果不出错,说明是测试通过了。

你可能感兴趣的:(作业笔记12_unit test)