Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the five ultra-practical Python automated testing frameworks?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)05/31 Report--

What are the five super-practical Python automated testing frameworks? I believe many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problems. Through this article, I hope you can solve this problem.

With the progress of technology and the emergence of automation technology, some automated testing frameworks have emerged in the market. Only by adjusting some applicability and efficiency parameters, these automated testing frameworks can be used out of the box, which greatly saves development time. And because these frameworks are widely used, they are robust and have a wide variety of use case sets and techniques to easily find minor defects. Today, we will take a look at the common Python automated testing framework.

Common testing frameworks

1 、 Unittest

Unittest is the standard class library built into Python. Its API is very similar to Java's JUnit and. Net's NUnit,C++ 's CppUnit.

Create a test case by inheriting from unittest.TestCase.

For example:

Import unittest

Def fun (x):

Return x + 1

Class MyTest (unittest.TestCase):

Def test (self):

Self.assertEqual (fun (3), 4)

After execution, it is successful.

However, if you change the expected result to 5, the result of execution is shown in the following figure:

2 、 Doctest

The doctest module searches for Python snippets that look like interactive sessions, then attempts to execute and validate the results. Even if we've never touched doctest, we can get a glimpse of the name. "it looks like a docstring in code." if you think so, you're half right.

For example:

Def square (x):

"" Squares x.

> square (2)

four

> > square (- 2)

four

> > square (5)

twenty-five

"

Return x * x

If _ _ name__ = ='_ _ main__':

Import doctest

Doctest.testmod ()

When the code is executed, the test code that follows the > in the document is executed and compared with the results of the next line. The results of the execution are as follows:

However, if we change the result and change the result of square (2) to 5, the test code is as follows:

Def square (x):

"" Squares x.

> square (2)

five

> > square (- 2)

four

> > square (5)

twenty-five

"

Return x * x

If _ _ name__ = ='_ _ main__':

Import doctest

Doctest.testmod ()

The test results performed are as follows:

3 、 py.test

Pytest is a unit testing framework of python, similar to the unittest testing framework that comes with python, but more concise and efficient than the unittest framework. According to the official website of pytest, it has the following characteristics:

① is very easy to use, easy to get started, rich in documentation, and there are many examples in the documentation for reference.

② can support simple unit testing and complex functional testing.

③ supports parameterization

④ can skip some tests during the execution of tests, or mark some case with expected failure as failure

⑤ supports repeated execution of failed case

⑥ supports running test case written by nose and unittest

⑦ has many third-party plug-ins and can be customized to extend

⑧ convenient integration with continuous integration tools

Write pytest test samples

Writing pytest test samples is very simple, as long as you follow the following rules (similar to nose):

The test file begins with test_ (or ends with _ test)

Test classes start with Test and cannot have init methods

The test function begins with test_

Asserting to use basic assert is fine

Example.py

Setup_class/teardown_class is executed at the beginning and end of the current test class.

Setup/treadown executes at the beginning and end of each test method.

Setup_method/teardown_method executes at the beginning and end of each test method, at the same level as setup/treadown.

Execute the pytest test sample

There are many ways to execute the test sample. The first example above is to execute py.test directly, and the second example is to pass the test file to py.test. In fact, there are many ways for py.test to perform tests:

4 、 Nose

Nose is an extension of unittest that makes python testing easier. Nose automatically discovers the test code and executes it. Nose provides a large number of plug-ins, such as xUnitcompatible of test output, overwriting reports, and so on.

Detailed documentation of nose: https:///latest/http://

5 、 Unittest2

It's an upgraded version of unitest. Improvements have been made to API and better diagnostic syntax.

Detailed documentation of unittest2: https://ittest2http://www./mock/

In other words, if you have your expectations for automated testing, then you must pay the corresponding price and effort. Good things also need good people to spend a lot of time to complete. Before officially entering the field of automated testing, it is necessary to establish such values in order to go further on the road of software testing.

After reading the above, have you mastered what are the five super-practical Python automated testing frameworks? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report