当前位置:   article > 正文

【HarmonyOS】HarmonyOS Test测试用例中一些断言API的使用_expect().assertfail()

expect().assertfail()

【关键词】

单元测试框架、HarmonyOS Test、assertThrowError、assertFail、assertEqual

【测试代码及测试结果展示】
这里以新建API9工程自动生成的ohosTest来编写单元测试代码。

1、  测试代码:

  1. import { describe, it, expect } from '@ohos/hypium'
  2. import abilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry';
  3. class CharacterReader {
  4. constructor() {
  5. }
  6. unconsume(){
  7. throw new Error("this is an error")
  8. }
  9. unconsume1(a: number,b: number){
  10. if (a == 1 && b == 2) {
  11. throw new Error("this is an error")
  12. }
  13. }
  14. add(a: number,b: number) {
  15. return a + b
  16. }
  17. }
  18. export default function abilityTest() {
  19. describe('ActsAbilityTest', function () {
  20. // 验证待验证方法的行为时,不带括号
  21. it('assertThrowError',0,() => {
  22. let reader = new CharacterReader()
  23. expect(reader.unconsume).assertThrowError("this is an error")
  24. })
  25. // expect验证行为不能带括号,目前想要传递参数就另行封装一个空参方法,再转而测试这个空参方法,如下
  26. it('assertThrowError', 0, () => {
  27. let reader = new CharacterReader();
  28. let a = 1;
  29. let b = 2;
  30. const test:Function = () => {
  31. reader.unconsume1(a,b)
  32. }
  33. expect(test).assertThrowError("this is an error")
  34. })
  35. // 验证待验证方法的行为时,带括号
  36. it('assertEqual',0,() => {
  37. let reader = new CharacterReader()
  38. expect(reader.add(1,1)).assertEqual(2)
  39. })
  40. // assertFail断言,使一个用例强制失败
  41. it('assertFail',0,() => {
  42. let reader = new CharacterReader()
  43. expect(reader.unconsume).assertFail()
  44. })
  45. })
  46. }

2、  测试结果

cke_13299.png

【参考文档】
1、https://developer.harmonyos.com/cn/docs/documentation/doc-guides/harmonyos_jnit_jsunit-0000001092459608

2、https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/unit-test-0000001507402510-V3#section160718461418

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/242317
推荐阅读
相关标签
  

闽ICP备14008679号