python目标检测入门
Testing code is always wanted as a good habit. when you are working on a project, It’s really a good idea to check that every single unit/program is working perfectly. There is a lot of modules to test code in python. In this article, I’m going to share some basic with coding examples to getting started with python unit testing.
牛逼 esting代码总是想作为一个良好的习惯。 当您在进行项目工作时,检查每个单元/程序是否工作正常是一个好主意。 有很多模块可以在python中测试代码。 在本文中,我将分享一些基本的编码示例,以开始python单元测试。
自动化与手动测试 (Automated vs. Manual Testing)
Testing is an integral part of any successful software project. Software Testing is categorized into two parts namely Automation Testing and Manual Testing.
测试是任何成功软件项目必不可少的部分。 软件测试分为两部分,即自动化测试和手动测试 。
手动测试 (Manual Testing)
Manual Testing is the process of testing the Software manually to find the defects before software goes live. This testing process is performed by a Quality Assurance engineer. In this process, testers execute test cases and generate reports manually without using any automation testing tools.
手动测试是在软件上线之前手动测试软件以发现缺陷的过程。 此测试过程由质量保证工程师执行。 在此过程中,测试人员无需使用任何自动化测试工具即可手动执行测试案例并生成报告。
There is some kind of manual testing processes used by the testers.
测试人员使用某种手动测试过程。
- Black Box Testing 黑匣子测试
- White Box Testing 白盒测试
- Unit Testing 单元测试
- System Testing 系统测试
- Integration Testing 整合测试
- Acceptance Testing 验收测试
I’m not going deep in each type of Manual Testing on this basic learning. I will try to explain these types in my next article.
在此基础学习中,我不会深入介绍每种类型的手动测试。 我将在下一篇文章中尝试解释这些类型。
自动化测试 (Automation Testing)
Automation Testing is the process of testing the software using an automation tool to find the defects. In this process, executing test scripts and generating reports are performed automatically. Some automation tools are:
自动化测试是使用自动化工具来发现缺陷的软件测试过程。 在此过程中,将自动执行测试脚本并生成报告。 一些自动化工具是:
- Selenium Selenium
- LoadRunner LoadRunner
- SilkTest 丝绸测试
- IBM Functional Tester IBM Functional Tester
![Image for post](https://miro.medium.com/max/9999/1*kj65XMPiH7-u0nAhFATI9A.png)
This comparison doesn't mean that you can choose one over the other. In the software industry, both of them are used sequentially. There is still a lot to know about Automation and Manual Testing.
这种比较并不意味着您可以选择一个。 在软件行业中,它们都是顺序使用的。 关于自动化和手动测试还有很多知识。
单元测试与集成测试 (Unit Testing vs. Integration Testing)
When you are working on a team, you may contribute a small part of the software/project. The combined works become a successful project. When you write a simple function and test that code is working fine, then this type of test is called a Unit test. When all of the modules are integrated and then testing is required to check that everything is working as expected, that’s called Integration Testing. So, a unit testing is to identify that a small component is working or not whereas integrated testing is to identify that component operates with each other components on the application.
在团队中工作时,您可以贡献一小部分软件/项目。 合并的作品成为一个成功的项目。 当您编写一个简单的函数并测试代码是否运行正常时,这种测试称为单元测试。 当所有模块都集成在一起后,需要进行测试以检查一切是否按预期进行,这称为Integration Testing 。 因此,单元测试是要确定一个小组件是否正常工作,而集成测试是要确定该组件与应用程序上的其他组件一起运行。
![Image for post](https://miro.medium.com/freeze/max/9999/1*wLa_2P7D0coUth2vZ7iUiw.gif)
单元测试: (Unit test:)
- identify a small piece of code is working as expected or not 确定一小段代码是否按预期工作
- checks a single component of the application/module 检查应用程序/模块的单个组件
- It comes under the white box testing type 它属于白盒测试类型
- test each unit separately and ensure that each unit is working as expected 分别测试每个单元并确保每个单元都按预期工作
整合测试 (Integration Testing)
- identify different modules are working as expected 确定不同的模块按预期工作
- It checks the overall flow of the application 它检查应用程序的整体流程
- comes under both white box and black box 在白框和黑框下
- It’s performed after unit testing 在单元测试之后执行
In the 1st part, I have explained some basics of software testing and its purposes. In the 2nd part, I am giving some coding examples basis on unit testing in python.
在第一部分中,我已经解释了软件测试的一些基础知识及其目的。 在第二部分中,我将给出一些基于python单元测试的编码示例。
In python programming, there are many test runners available. unittest
is one of them which one is built into python standard library. Some other test runners are:
在python编程中,有许多可用的测试运行器。 unittest
是其中之一,它内置在python标准库中。 其他一些测试人员是:
doctest
doctest
pytest
pytest
unittest
(unittest
)
unittest
is a python built-in standard library. unittest
contains both a testing framework and a test runner. To write and execute tests, it has some requirements. whiches are
unittest
是python内置的标准库。 unittest
包含一个测试框架和一个测试运行器。 要编写和执行测试,它有一些要求。 这是
- You need to put your test cases inside a class as methods 您需要将测试用例作为方法放入类中
You have to use assertion methods in the
unittest
class您必须在
unittest
类中使用断言方法
![Image for post](https://miro.medium.com/max/9999/1*9kSsBsn__CMQLEhRUcre6g.png)
unittest
assertion methods
unittest
断言方法
To execute a test you have to:
要执行测试,您必须:
import
unittest
library导入
unittest
库create a class that inherits from
unittest.TestCase
创建一个从
unittest.TestCase
继承的类define test method with
self
as an argument以
self
为参数定义测试方法use
self.assertion
series to perform test execution使用
self.assertion
系列执行测试执行To generates the test results run
unittest.main()
inside the main function要生成测试结果,请在main函数内部运行
unittest.main()
That’s all, you will get OK
if there is no error, see the below example.
就是这样,如果没有错误,您将获得OK
,请参见以下示例。
Doctest
(Doctest
)
The doctest
also a python built-in library. This module searches for pieces of text that look like interactive Python sessions in docstrings and then execute those sessions to verify that they work exactly as shown.
doctest
也是python内置库。 该模块在文档字符串中搜索看起来像交互式Python会话的文本片段,然后执行这些会话以验证它们是否按所示正常工作。
Doctests
have a different use case than proper unit tests: they are usually less detailed and don’t catch special cases or obscure regression bugs. They are useful as expressive documentation of the main use cases of a module and its components. However, doctests
should run automatically each time the full test suite runs.
Doctests
与正确的单元测试有不同的用例:它们通常不那么详细,并且不会捕获特殊情况或模糊的回归错误。 它们可用作模块及其组件的主要用例的表达性文档。 但是,每次完整的测试套件运行时, doctests
应该自动运行。
A simple doctest
in a function
函数中的简单doctest
While running this code, doctest
will run and complain if the given description doesn't work as expected.
在运行此代码时, doctest
将运行并抱怨给定的描述是否无法按预期工作。
pytest
(pytest
)
pytest
is an external tool for testing in python. It has some advantages compared to unittest
. Since it is an external tool, so you have to install it first in your python interpreter. To install pytest
run the following command:
pytest
是用于在python中进行测试的外部工具。 与unittest
相比,它具有一些优势。 由于它是外部工具,因此您必须首先在python解释器中安装它。 要安装pytest
运行以下命令:
pip install pytest
following code to understand how it works:
以下代码了解其工作原理:
Run the following command in your project terminal:
在项目终端中运行以下命令:
py.test pytest.py
![Image for post](https://miro.medium.com/max/9999/1*bMc8Y2mWYKznqjmwgIZRAw.png)
You will get results like the above image.
您将获得如上图所示的结果。
We see that, pytest
doesn’t need any TestClass, we can use simply assert
to check test cases and execute the test cases directly from the terminal to generate reports.
我们看到, pytest
不需要任何TestClass,我们可以使用简单的assert
来检查测试用例,并直接从终端执行测试用例以生成报告。
You can use whatever you like and depending on your requirements. You can share with us which python module you are using currently.
您可以根据需要使用任何您喜欢的东西。 您可以与我们分享您当前正在使用哪个python模块。
Thanks for reading. Thumbs up if you like it. Also if you have any suggestion regarding this article, comments below.
谢谢阅读。 如果喜欢,请竖起大拇指。 另外,如果您对本文有任何建议,请在下面评论。
翻译自: https://medium.com/big0one/getting-started-with-testing-in-python-c21e79c2b252
python目标检测入门