首页 > 其他分享 >pytest

pytest

时间:2022-09-30 22:55:43浏览次数:65  
标签:tests code testing pytest test unit

pytest

https://docs.pytest.org/en/7.1.x/contents.html

# content of test_sample.py
def func(x):
    return x + 1


def test_answer():
    assert func(3) == 5

 

A hands-on guide to unit testing

https://www.datacamp.com/tutorial/pytest-tutorial-a-hands-on-guide-to-unit-testing

 

TDD

More recently, an interest in test-driven development (TDD) has grown significantly among developers. Diving into the depths of it may be a complex task for this article, but the general idea is that the traditional process of development and testing is reversed - you write your unit tests first, and then implement code changes until the tests pass.

 

UT

The idea behind these tests is to permit developers to isolate the smallest unit of code that makes logical sense and test that it behaves as expected. In other words, unit tests validate that a software program's single component works as the developers intended. 

Ideally, these tests should be pretty small - the smaller they are, the better. One reason for building smaller tests is that the test will be more efficient since testing smaller units will enable the testing code to execute much faster. Another reason for testing smaller components is that it gives you greater insight into how the granular code behaves when merged.

 

WHY UT?

The global justification for why it’s essential to conduct unit tests is that developers must ensure the code they write meets the quality standards before permitting it to enter a production environment. However, several other factors contribute to the necessity of unit tests. Let’s dig deeper into some of those reasons.

Preserves resources 

Carrying out unit tests help developers catch code bugs during the software construction stage, preventing them from transitioning deeper into the development lifecycle. This preserves resources since developers wouldn’t have to pay the cost of fixing bugs later in development - it also means end-users are less likely to have to deal with buggy code.  

Extra documentation 

Another great justification for conducting unit tests is that they serve as an extra layer of living documentation for your software product. Developers can simply refer to the unit tests for a holistic understanding of the overall system since they detail how the more minor components should behave. 

Confidence boost

It’s extremely simple to make subtle mistakes in your code while writing up some functionality. However, most developers would agree it’s much better to identify the breaking points within a codebase before it’s put in a production environment: unit tests provide developers with this opportunity. 

It’s fair to say that “code covered with unit tests can be considered more reliable than code that is not.” Future breaks in the code may be discovered much faster than code with no test coverage, saving time and money. Developers also benefit from extra documentation so they can understand the codebase quicker, and there is the added confidence of knowing that if they make a mistake in their code, it will be caught by the unit tests rather than by an end-user.

framework

Python has grown tremendously in popularity over the years. As part of Python’s growth, the number of testing frameworks has also increased, resulting in a wealth of tools available to help you test your Python code. Getting into the nitty-gritty of each tool is beyond the scope of this article, but we will touch on some of the most common Python testing frameworks available. 

unittest

Unittest is a built-in Python framework for unit testing. It was inspired by a unit testing framework called JUnit from the Java programming language. Since it comes out of the box with the Python language, there are no extra modules to install, and most developers use it to begin learning about testing.  

Pytest

Pytest is possibly the most widely used Python testing framework around - this means it has a large community to support you whenever you get stuck. It’s an open-source framework that enables developers to write simple, compact test suites while supporting unit testing, functional testing, and API testing.

doctest

The doctest framework merges two core components of software engineering: documentation and testing. This functionality ensures that all software programs are thoroughly documented and tested to ensure they run as they should. doctest comes with Python’s standard library and is pretty straightforward to learn. 

nose2

Nose2, the successor to the nose regiment,  is essentially unittest with plugins. People often refer to nose2 as “extended unit tests” or “unit tests with a plugin” due to its close ties to the Python built-in unit testing framework. Since it’s practically an extension of the unittest framework, nose2 is incredibly easy to adopt for those familiar with unittest.

Testify

Testify, a Python framework for unit, integration, and system testing, is popularly known as the framework that was designed to replace unittest and nose. The framework is packed with extensive plugins and has quite a smooth learning curve if you’re already familiar with unittest.

Hypothesis

Hypothesis enables developers to create unit tests that are simpler to write and are powerful when run. Since the framework is built to support data science projects, it helps to find edge cases that aren’t so apparent while you’re creating your tests by generating examples of inputs that align with specific properties you define.

For our tutorial, we will be using pytest. Check out the next section to see why you may wish to opt for Pytest over the others we’ve listed.

WHY PYTEST?

好得多的用户体验。

极大减少了执行测试的代码量。

Beyond its vast supportive community, pytest has several factors that make it one of the greatest tools to conduct your automated test suite in Python. Pytest’s philosophy and features are set up to make software testing a much better developer experience. One way the creators of Pytest achieved this goal is by significantly reducing the amount of code required to perform common tasks and making it possible to perform advanced tasks with extensive commands and plug-ins. 

Some other reasons to use Pytest include the following: 

Easy to learn 

Pytest is extremely easy to learn: if you understand how Python’s assert keyword works, then you’re already well on your way to mastering the framework. Tests using pytest are Python functions with “test_” prepended or “_test” appended to the function's name - although you can use a class to group multiple tests. Overall, the learning curve for pytest is much shallower than the likes of unittest since you’re not required to learn any new constructs. 

Test filtering

You may not want to run all of your tests with each execution - this may be the case as your test suite grows. Sometimes, you may wish to isolate a few tests on a new feature to get rapid feedback while you’re developing, then run the full suite once you’re confident everything is functioning as planned. Pytest has three ways you could isolate tests: 1) name-based filtering, which tells pytest only to run the tests whose names match the pattern provided 2) directory scoping, which is a default setting that tells pytest to only run tests that are in or under the current directory and 3) test categorization which allows you to define categories for tests that pytest should include or exclude. 

Parameterization

Pytest has a built-in decorator called parametrize that enables the parametrization of arguments for a test function. Thus, if the functions you’re testing process data or performs a generic transformation, you are not required to write several similar tests. We will cover more on parametrization later in the article. 

We will stop here, but the list of why pytest is a great tooling option for your automated test suite goes on.

 

Reference

https://realpython.com/pytest-python-testing/#toc

https://www.tutorialspoint.com/pytest/pytest_quick_guide.htm

 

标签:tests,code,testing,pytest,test,unit
From: https://www.cnblogs.com/lightsong/p/16746465.html

相关文章

  • 每天进步一点点-pytest插件介绍
    pytest-html插件使用测试结果生成HTML报告安装pip3installpytest-html-ihttp://pypi.douban.com/simple/--trusted-hostpypi.douban.com快速入门pytest......
  • python学习笔记:pytest单元测试框架
    一、安装配置和运行规则1、安装:pipinstallpytest查看安装版本:pytest--version 2、Pytest用例运行规则用Pytest写用例时候,一定要按照下面的规则去写,否则不符合规......
  • Pytest+allure+requests接口自动化
    实现功能测试数据隔离:测试前后进行数据库备份/还原接口直接的数据依赖:需要B接口使用A接口响应中的某个字段作为参数对接数据库:讲数据库的查询结果可直接用于断言......
  • 基于pytest接口自动化二 之 生成allure报告
    六、生成allure报告安装allure官网下载allure,https://github.com/allure-framework/allure2/releases配置环境变量:mac:终端输入open-t~/.bash_profile;配置环......
  • pytest测试框架2【控制用例的执行顺序】
    1.pytest加载所有的测试用例都是乱序的,如果想指定用例的顺序,可以使用pytest-ordering插件,指定用例的执行顺序只需要在测试用例的方法前面加上装饰器@pytest.mark.run(order......
  • pytest+allure 之测试报告
     'allure'不是内部或外部命令,也不是可运行的程序或批处理文件。如果运行生成allure报告时,报此错误,大多是没有配置allure环境变量;具体如何配置,见下面文章详情;  if......
  • pytest测试框架(一)初识
    简介pytest是一个全功能的python的测试工具,与python自带的unittest测试框架类似,但pytest使用起来更加的简洁和高效,并兼容unittest框架。pytest可以结合requests实现接口测......
  • pytest初理
    接口自动化测试pythonjson.loads()#把json转换为字符串格式json.dumps#把字符串转换为json格式pytest用户管理框架一.pytest单元测试框架什么是单元测试框架......
  • pytest 钩子函数 (二) 用例收集钩子
    pytest_collectiont设置pytest收集用例执行的流程,这个钩子函数一般不需要重写,除非你想自己制定pytest用例收集的流程。参数session:pytest会话对象触发时机:......
  • 全网最全最新的Pytest框架快速进阶篇
     一、Pytest的前置和后置方法1.Pytest可以集成unittest实现前置和后置importunittestimportpytest classTestCase(unittest.TestCase):   defsetUp(self)......