使用xmlrunner的Python单元测试

时间:2018-06-21 15:22:31

标签: python xml unit-testing testing python-unittest

晚上好。 python unittest有小问题。我需要将测试结果输出到xml文件。我这样做了,但是控制台输出丢失了。需要在控制台+ xml输出中复制结果。怎么办?

不要注意测试本身。只需底部

import json
import requests
import unittest
import configparser
import xmlrunner

class Test(unittest.TestCase):

 def test_User(self):
    config = configparser.ConfigParser()
    config.read('..//settings.ini')
    config.sections()
    url = config['API']['url'] + 'user/temp'
    print('\n---User - temp registration---' + '\nURL: ' + url)
    headers = {'Content-type': 'application/json',
               'Accept': 'text/plain',
               'Content-Encoding': 'utf-8'}
    data = {"deviceId": "1",
            "idfa": "1",
            "gaid": "1"}
    post(data, url, headers)

#Post request
def post(data, url, headers):
   print('Request: ' + str(data))
   answer = requests.post(url, data=json.dumps(data), headers=headers)
   print(str(answer) + '\nResponse: ' + str(answer.json()))

if __name__ == "__main__":
   with open('results.xml', 'wb') as output:
    unittest.main(
        testRunner=xmlrunner.XMLTestRunner(output=output),
        failfast=False, buffer=False, catchbreak=False)

Screenshot of test results with xmlrunner.

Screenshot of test results without xmlrunner.

需要两个结果:控制台输出+ xml输出

0 个答案:

没有答案