【接口自动化】xml响应断言

3/29/2023 接口自动化

# xml断言

from requests_xml import XMLSession
session = XMLSession()
r = session.get('https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss')	
r.xml.links
1
2
3
4

# xpath断言

from requests_xmlimport XMLSession
session = XMLSession()
r = session.get('https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss')	
r.xml.links
item = r.xml.xpath('//item', first=True)
print(item.text)
1
2
3
4
5
6

# 直接解析xml

# xml解析
import xml.etree.ElementTree as ET
root = ET.fromstring(countrydata)
root.findall(".")
root.findall("./country/neighbor")root.findall(".//year/..[@name='Singapore']")
root.findall(".//*[@name='Singapore']/year")
root.findall(".//neighbor[2]")
1
2
3
4
5
6
7

# Hamcrest断言体系

Hamcrest可以处理多种复杂断言,实现逻辑序列,字典等功能的自定义断言,功能强大。

框架自带assert体系: assert,assertEqual Hamcrest体系: assertThat

from hamcrest import *
import unittest


class BiscuitTest(unittest.TestCase):
    def testEquals(self):
        theBiscuit = Biscuit("Ginger")
        myBiscuit = Biscuit("Ginger")
        assert_that(theBiscuit, equal_to(myBiscuit))


if __name__ == "__main__":
    unittest.main()
1
2
3
4
5
6
7
8
9
10
11
12
13
上次更新: 2025年02月04日 13:37:36