美文网首页
作业笔记12_unit test

作业笔记12_unit test

作者: ChZ_CC | 来源:发表于2017-02-07 14:09 被阅读97次
  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()

结果输出:

疑问:

#使用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

    编写测试,检查任意一个百科词条页面是否有摘要内容 编写测试,检查任意一个百科词条页面是否有目录内容 代码部分 运行...

  • R使用笔记:相关系数:cor.test();corr.test(

    本次笔记内容:cor.test()和cor()rcorr() {Hmisc}corr.test() {psych}...

  • 2018-02-05

    Go学习笔记一 This is a test.

  • 2018-12-04

    笔记 test 引用 一篇文章

  • test测试第一篇

    test小帅哥的乐高作业

  • __block和非__block

    自己的笔记: NSString *text = @"test"; testBlock block = ^{ dis...

  • Linux内核程序编写初试hello world

    本文为学习笔记,资料来自网络 1 *.c文件的编写$mkdir test 创建实验目录,例如test,名字随...

  • UI Test 笔记

    测试的目的是保证代码的质量和发布时的信心,以加速开发和迭代的效率;但是如果测试本身太过于难写复杂的话,反而会拖累开...

  • 9月24日

    化学笔记 化学作业 英语作业 语文作业 物理作业

  • 9月24日

    化学笔记 化学作业 英语作业 语文作业 物理作业

网友评论

      本文标题:作业笔记12_unit test

      本文链接:https://www.haomeiwen.com/subject/wbapittx.html