Compare assert exception in should(JavaScript) and unittest(Python)

简介:

should

With should, exception is asserted like this:

it('should throw when n isnt Number', function () {
    (function () {
      main.fibonacci('abcd');
    }).should.throw('n should be a Number');
  });

This library adds an attribute should to Object. The should attribute includes a bunch of assert functions. With this, you can do (5).should.above(3). It will be even harder in Python, because types defined in C cannot be monkeypatched. The implement of should.throw is a simple try/catch.

unittest

With assertRaises in unittest, exception is asserted like this:

with self.assertRaisesRegexp(ValueError, 'literal'):
    int('XYZ')

It's much more simple than another way, because we don't need to pass test function as an argument to the assert function, no matter the argument called this lol.
Exception is not catched by a try/except statement, but in __exit__ context manager.



目录
相关文章
|
1月前
|
JSON JavaScript 前端开发
在Python中调用和执行JavaScript
在Python中调用和执行JavaScript主要通过`PyExecJS`库实现。安装库后,可以使用`execjs.compile`编译JS代码并用`eval`或`call`执行。此外,还能加载JavaScript库和框架,调用外部JS文件,处理返回值,以及在两者间传递数据。Python和JavaScript各有优劣,适用于不同场景,结合使用可增强项目功能和灵活性。
48 0
|
1月前
|
前端开发 关系型数据库 MySQL
基于python+django+vue.js开发的社区养老管理系统
基于python+django+vue.js开发的社区养老管理系统
151 1
|
4天前
|
数据采集 前端开发 JavaScript
Python爬虫技术:动态JavaScript加载音频的解析
Python爬虫技术:动态JavaScript加载音频的解析
|
1月前
|
前端开发 JavaScript TensorFlow
如何将训练好的Python模型给JavaScript使用?
本文介绍了如何将TensorFlow模型转换为Web格式以实现浏览器中的实际应用。首先,简述了已有一个能够检测扑克牌的TensorFlow模型,目标是将其部署到Web上。接着,讲解了TensorFlow.js Converter的作用,它能将Python API创建的GraphDef模型转化为TensorFlow.js可读取的json格式,用于浏览器中的推理计算。然后,详细说明了Converter的安装、用法及不同输入输出格式,并提供了转换命令示例。最后,文中提到了模型转换后的实践步骤,包括找到导出的模型、执行转换命令以及在浏览器端部署模型的流程。
28 3
|
19天前
|
测试技术 Python
【Python自动化测试】:Unittest单元测试与HTMLTestRunner自动生成测试用例的好帮手
【Python自动化测试】:Unittest单元测试与HTMLTestRunner自动生成测试用例的好帮手
13 0
|
1月前
|
编解码 JavaScript 前端开发
【专栏】介绍了字符串Base64编解码的基本原理和在Java、Python、C++、JavaScript及Go等编程语言中的实现示例
【4月更文挑战第29天】本文介绍了字符串Base64编解码的基本原理和在Java、Python、C++、JavaScript及Go等编程语言中的实现示例。Base64编码将24位二进制数据转换为32位可打印字符,用“=”作填充。文中展示了各语言的编码解码代码,帮助开发者理解并应用于实际项目。
|
1月前
|
测试技术 Python
Python测试架构unittest
【4月更文挑战第19天】
12 3
|
1月前
|
Java 测试技术 数据库连接
【如何学习Python自动化测试】—— Python 的 unittest 框架
【如何学习Python自动化测试】—— Python 的 unittest 框架
11 0
|
1月前
|
编解码 JavaScript 前端开发
python如何解决js逆向混淆?
python如何解决js逆向混淆?
25 0
|
1月前
|
前端开发 测试技术 C++
Python自动化测试面试:unittest、pytest与Selenium详解
【4月更文挑战第19天】本文聚焦Python自动化测试面试,重点讨论unittest、pytest和Selenium三大框架。unittest涉及断言、TestSuite和覆盖率报告;易错点包括测试代码冗余和异常处理。pytest涵盖fixtures、参数化测试和插件系统,要注意避免过度依赖unittest特性。Selenium的核心是WebDriver操作、等待策略和测试报告生成,强调智能等待和元素定位策略。掌握这些关键点将有助于提升面试表现。
61 0