卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章79892本站已运行4417

使用 Sheepy 在 Python 中进行单元测试

使用 sheepy 在 python 中进行单元测试

大家好,今天我来给大家介绍一个新的单元测试库,叫做sheepy,但是首先我们来谈谈单元测试的重要性。该库不适合初学者,要使用它进行单元测试,您需要额外注意。它仅具有用于使用端点和 http 错误检查模块进行 api 测试的断言。

github链接:github
pypi 链接:pypi

生产中所有成熟、有自尊的软件都有单元测试,无论是为了了解代码中已有的内容是否仍然有效,为了防止之前已经报告和修复的错误,还是为了测试新功能,它很好地表明他们正在向前推进,并且没有积累技术债务。我们以火狐浏览器为例,每个目录下有一个tests子目录,针对已经报告的bug进行专门的测试,这样就保证了已经修复的bug不会再凭空出现,已经修复的bug就会出现又无处可去 这叫扔钱。随着时间的推移,您将失去时间、金钱、效率和市场份额,而竞争对手却比您做得更好,资源更少。

每个感觉自己无能为力的人都会试图诽谤那件事,单元测​​试也不例外。创建覆盖每个用例的更好的单元测试需要时间,就像生活中的一切一样,你的后端我怀疑你只阅读了一个教程并做出了完美的 api,对于你的前端来说也是如此,我怀疑你看了一门课程并来了使界面变得完美。所以不要认为单元测试会有什么不同!

断言方法

+-----------------------+-------------------------------------------------------+
| assertion method       | description                                           |
+-----------------------+-------------------------------------------------------+
| assertequal(a, b)      | checks if two values are equal.                       |
| assertnotequal(a, b)   | checks if two values are not equal.                   |
| asserttrue(expr)       | verifies that the expression is true.                 |
| assertfalse(expr)      | verifies that the expression is false.                |
| assertraises(exc, fn)  | asserts that a function raises a specific exception.  |
| assertstatuscode(resp) | verifies if the response has the expected status code.|
| assertjsonresponse(resp)| confirms the response is in json format.             |
| assertresponsecontains(resp, key) | ensures the response contains a given key. |
+-----------------------+-------------------------------------------------------+

安装

立即学习“Python免费学习笔记(深入)”;

安装非常简单,只需打开您选择的终端,安装 pip 并输入 pip install sheepy

使用示例

from sheepy.sheeptest import sheepytestcase

class exampletest(sheepytestcase):
    def test_success(self):
        self.asserttrue(true)

    def test_failure(self):
        self.assertequal(1, 2)

    def test_error(self):
        raise exception("forced error")

    @sheepytestcase.skip("reason to ignore")
    def test_skipped(self):
        pass

    @sheepytestcase.expectedfailure
    def test_expected_failure(self):
        self.assertequal(1, 2)

sheepytestcase 类提供了多种用于创建和执行单元测试的功能,包括用于配置特殊行为的断言方法和机制,例如跳过测试或处理预期的失败。

在exampletest类中,定义了五个测试方法:

test_success:此测试检查传递给asserttrue方法的表达式是否为true。由于 true 值已显式传递,因此此测试将成功。

test_failure:此测试使用assertequal方法检查两个值之间的相等性。然而,比较值1和2不同,导致测试失败。这演示了预期失败的情况,其中测试必须检测到不一致。

test_error:此方法会引发一个有目的的异常,并显示消息“强制错误”。目标是测试系统在处理测试执行期间发生的错误时的行为。由于该方法抛出异常而不对其进行处理,因此测试结果将是错误。

test_skipped:此测试已使用 sheepytestcase 类的 skip 方法进行修饰,这意味着在运行测试时将跳过它。跳过测试的原因被提供为“忽略的原因”,并且这个理由可以在最终的测试报告中显示。

test_expected_failure:该方法使用expectedfailure装饰器,表示预计会发生失败。在方法内部,在 1 和 2 之间存在相等性检查,这通常会导致失败,但是随着装饰器的应用,框架认为这种失败是预期行为的一部分,不会被视为错误,但是作为“预期的失败”。

输出


测试结果:
exampletest.test_error:失败 - 强制错误
exampletest.test_expected_failure:预期失败
exampletest.test_failure: fail - 1 != 2
exampletest.test_skipped: 跳过 -
exampletest.test_success: 好的

api 测试用例

sheepy 测试框架中的 api 测试被设计得简单而强大,允许测试人员使用常见的 http 方法(如 get、post、put 和 delete)与 api 进行交互。该框架提供了一个专用类 apirequests 来简化发送请求和处理响应,并通过 httperror 异常类进行内置错误管理。

测试api时,测试类继承自sheepytestcase,它配备了各种断言方法来验证api的行为。其中包括用于验证 http 状态代码的assertstatuscode、用于确保响应采用 json 格式的assertjsonresponse 以及用于检查响应正文中是否存在特定键的assertresponsecontains。

例如,该框架允许您向 api 发送 post 请求,验证状态代码是否与预期值匹配,并断言 json 响应包含正确的数据。 api 请求通过 apirequests 类进行处理,该类负责构建和发送请求,而当服务器返回意外状态代码时,通过引发 http 特定错误来简化错误处理。

通过提供内置断言和错误处理,该框架可以自动执行 api 测试中的大部分重复任务,确保编写测试的正确性和简单性。该系统可以让开发者专注于验证api行为和逻辑,成为保证api交互可靠性的有效工具。

from sheepy.sheeptest import sheepytestcase  

class testhttpbinapi(sheepytestcase):
    def __init__(self):

        super().__init__(base_url="https://httpbin.org")

    def test_get_status(self):

        response = self.api.get("/status/200")
        self.assertstatuscode(response, 200)  

    def test_get_json(self):

        response = self.api.get("/json")
        self.assertstatuscode(response, 200)  
        self.assertjsonresponse(response)  
        self.assertresponsecontains(response, "slideshow")  

    def test_post_data(self):

        payload = {"name": "sheepytest", "framework": "unittest"}
        response = self.api.post("/post", json=payload)
        self.assertstatuscode(response, 200)  
        self.assertjsonresponse(response)  
        self.assertresponsecontains(response, "json") 
        self.assertequal(response.json()["json"], payload)  

    def test_put_data(self):

        payload = {"key": "value"}
        response = self.api.put("/put", json=payload)
        self.assertstatuscode(response, 200)  
        self.assertjsonresponse(response)  
        self.assertresponsecontains(response, "json")  
        self.assertequal(response.json()["json"], payload)  

    def test_delete_resource(self):

        response = self.api.delete("/delete")
        self.assertstatuscode(response, 200)  
        self.assertjsonresponse(response)  

输出示例

Test Results:
TestHttpBinApi.test_delete_resource: OK
TestHttpBinApi.test_get_json: OK
TestHttpBinApi.test_get_status: OK
TestHttpBinApi.test_post_data: OK
TestHttpBinApi.test_put_data: OK

摘要:

新的sheepy库是一个令人难以置信的单元测试库,它有多种测试接入方法,其中包括一个仅用于api测试的模块,在我看来,它不是一个适合初学者的库,它需要面向对象编程的基础知识例如方法、类和继承。

卓越飞翔博客
上一篇: 在 Perl 和 Go 中探索密码强度和数字验证
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏