通过请求检查远程呼叫

MonitorRequests的Python项目详细描述


Build StatusPyPI

通过requests模块检查远程调用的工具。

找出您的代码正在调用的外部api。

可选:
  • 找到打电话的地方。
  • 捕获响应和响应状态代码。
  • 按域筛选
  • 作为并行测试的服务器运行

用法

importunittestimportmonitor_requestsfromexampleimportexample_methodclassExampleTestCase(unittest.TestCase):@classmethoddefsetUpClass(cls):cls.monitor=monitor_requests.Monitor()@classmethoddeftearDownClass(cls):print('External calls for example:')# Optionally pass tracebacks=False to skip outputing tracebacks# Report will call .stop() by default.cls.monitor.report(tracebacks=False)deftest_example_method(self):result=example_method()self.assertEqual(result.status_code,200)if__name__=='main':unittest.main()

要筛选捕获的域:

# Only capture facebook and google requests:monitor=monitor_requests.Monitor(domains=['facebook\.com','google\.com'])

要在django测试运行程序中设置此项: (如果以串行方式运行测试,则此操作仅在套件级别有效。根据您的设置,您可能需要使用–parallel=1)运行。另外,还有一些关于如何使用服务器模式异步地将数据推送到包含的tornado数据服务器的说明。

defrun_suite(self,suite,**kwargs):monitor=monitor_requests.Monitor()test_result=super(ReelioTestRunner,self).run_suite(suite,**kwargs)monitor.report()returntest_result

在py.test会话设备中设置:

@pytest.fixture(scope='session')defsession_fixture():monitor=monitor_requests.Monitor()yieldmonitor.report()

写入文件:

@classmethoddeftearDownClass(cls):withopen('output.txt','w')asf:cls.monitor.report(output=f)

对于输出的微调控制:

  • 使用debug=true显示URL、响应和回溯。
  • 使用urls=true显示URL。
  • 使用tracebacks=truerespones=true显示回溯或响应(也将显示URL,回溯和响应都按URL组织)。

*服务器模式*

如果要激活对并行运行的整个测试套件的监视器请求,可以运行包含的tornado服务器来保存请求数据:

monitor_requests_server --port=9003
defrun_suite(self,suite,**kwargs):# Make sure to turn off mocking at the suit or session level.monitor=monitor_requests.Monitor(server_port=9003,mock=False)test_result=super(ReelioTestRunner,self).run_suite(suite,**kwargs)monitor.report()returntest_result

您需要在测试用例类中执行其他调用:

classExampleTestCase(unittest.TestCase):@classmethoddefsetUpClass(cls):# Same port, and same domain filtering if applicable.cls.monitor=monitor_requests.Monitor(server_port=9003)@classmethoddeftearDownClass(cls):# Make sure to stop the mocking in the tear down.cls.monitor.stop()

注意,这里没有teardownclass,也没有对stop()或report()的调用。 这只发生在会话级别。

安装

pip install MonitorRequests

示例输出

使用debug=true

__________URLS__________

__________URL________
URL:      http://facebook.com?param=test
Requests: 1
______Tracebacks_____
File "example.py", line 22, in <module>
    run()
  File "example.py", line 18, in run
    get_function_fb()
  File "example.py", line 12, in get_function_fb
    return requests.get('http://facebook.com?param=test')
_______Responses______
<StatusCode>200</StatusCode>
<Content><!DOCTYPE html>
<html lang="en" id="facebook" class="no_js">Etc/Etc</html></Content>

__________URL________
URL:      http://google.com
Requests: 1
______Tracebacks_____
File "example.py", line 22, in <module>
    run()
  File "example.py", line 17, in run
    get_function()
  File "example.py", line 7, in get_function
    return requests.get('http://google.com')
_______Responses______
<StatusCode>200</StatusCode>
<Content><!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en">Etc/Etc</html></Content>

___________Analysis__________

Total Requests: 2
Time (Seconds): 1.16714
URL Count:      2
Domain Count:   2
Domains:        facebook.com, google.com

使用debug=false

___________Analysis__________

Total Requests: 2
Time (Seconds): 1.08454
URL Count:      2
Domain Count:   2
Domains:        facebook.com, google.com

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在netbeans GUI中使用checknode创建JTree?   java事件无限循环SWT   java Thymeleaf空值检查   javajavax。同样是加密JDK源代码   java getColumnIndex访问外部内存中的特定文件夹   无法在Java代码中打开特定的JAR文件   java如何使用安全约束来保护url的一部分?   windows如何从命令行安装/删除Java快速启动服务   JavaSpring数据JPA保存新实体并获取其所有依赖项   swing Java JButton大小问题   IF语句可以在数学中使用吗。麦克斯在爪哇?   java如何使用POI设置空白excel单元格   java在签名为return int的方法中返回null?   java思想,如何将多个mircoservice项目放在同一个窗口中   直接从intelliJ在系统终端上运行java程序?   java通过POST从React应用程序向Spring Boot发送数组