【问题标题】:@unittest.skip(reason) can't work on main function@unittest.skip(reason) 不能在主函数上工作
【发布时间】:2022-06-27 22:12:34
【问题描述】:

我想在 python 中运行其他测试用例时防止/跳过一些测试用例。我无法在我的案例中使用@unittest.skip(reason)。它总是在 python unittest 中生成一个脚本错误。

我的代码;

import unittest
@unittest.skip("something")
def main():
    
    try:
        something = []
        for _ in range(4):
             test.log("something happened")

结果是;

Error Script Error 
    Detail: SkipTest: something

你对这个问题有什么想法吗?

【问题讨论】:

  • main 是一个测试函数,还是您正在测试的函数? unittest.skip 应该应用于测试功能。还有,test是什么,main函数里还有什么?
  • 测试用例的主要测试。我实际上有不同的结构。例如,我分别有 10 个测试用例(意味着 10 个不同的文件),并且两者都是在 main 函数下编写的。测试是 Squish IDE 的库。对于这种情况,这并不重要。

标签: python python-unittest squish


【解决方案1】:

Unittest 是一个充当测试框架的模块。您应该根据框架实践(取自 tutorialspoint.com)使用它:

import unittest

   def add(x,y):
      return x+y

class SimpleTest(unittest.TestCase):
   @unittest.skip("demonstrating skipping")
   def testadd1(self):
      self.assertEquals(add(4,5),9)

if __name__ == '__main__':
   unittest.main()

【讨论】:

  • 我已经试过了。但不幸的是,它不适用于我的情况。因为我没有班级结构。我的代码直接以; def main():
  • 据我所知框架,根据定义,没有类结构就不能使用unittest
  • 也许这就是我在我的框架中无法使用单元测试库的跳过方法的原因
【解决方案2】:

用 Python 编写的 Squish 测试脚本与 unittest 模块无关。我不知道如何将两者一起使用。

Squish 提供了test.skip() 函数来跳过测试用例:

test.skip(消息);

test.skip(消息,详细信息);

这个函数跳过当前脚本测试用例或当前 BDD 场景(或 BDD 场景大纲迭代)的进一步执行,方法是抛出一个异常并使用给定的消息字符串向 Squish 的测试日志添加一个 SKIPPED 条目,并使用提供详细信息(如果提供)。如果测试用例在跳过之前记录了验证失败,则仍然认为它失败。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-19
    相关资源
    最近更新 更多