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 common testing frameworks for python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces "what are the five common testing frameworks of python" in detail, with detailed contents, clear steps and proper handling of details. I hope this article "what are the five common testing frameworks of python" can help you solve your doubts.

one。 Unittest

Similar to JUnit, unittest is the standard unit testing framework for python, so it is sometimes called PyUnit. It is similar to other members of the xUnit family. It is also used by more people. Compatible with python2 and python3.

Personally, I prefer to use this. I have mainly used JUnit before, and I can get started with this quickly. And it belongs to python automatic integration, without additional installation package, it feels like you have everything you should have, and it is convenient to use.

Official website example:

Press Ctrl+C to copy the code

Import unittest

Class TestStringMethods (unittest.TestCase):

Def test_upper (self): self.assertEqual ('foo'.upper (),' FOO')

Def test_isupper (self): self.assertTrue ('FOO'.isupper ()) self.assertFalse (' Foo'.isupper ())

Def test_split (self): s = 'hello world' self.assertEqual (s.split (), [' hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises (TypeError): s.split (2)

If name = 'main': unittest.main ()

two。 Unittest2

Unittest2 can be said to be a patch for new features of the unittest testing framework. It is similar to unittest to a large extent. Then add some methods that are not available in unittest.

three。 Pytest

After taking a look at it, the pytest document is quite detailed. One of the concerns is that pytest can be parameterized directly through @ pytest.mark.parametrize, while unittest needs to be parameterized with DDT.

Official website example:

# content of test_sample.py

Def inc (x):

Return x + 1

Def test_answer ():

Assert inc (3) = = 5

Perform as follows:

$pytest

= test session starts =

Platform linux-- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y

Rootdir: $REGENDOC_TMPDIR, inifile:

Collected 1 item

Test_sample.py F

= FAILURES =

_ test_answer _

Def test_answer ():

> assert inc (3) = = 5

E assert 4 = = 5

E + where 4 = inc (3)

Test_sample.py:5: AssertionError

= 1 failed in 0.12 seconds = 4. Nose

Nose extends unittest to make testing easier.

Generally, use cases can be written in unittest, and then executed with nose. The test collection method of nose is still very convenient.

Specifically, nose can use @ with_setup () to define the setup and teardown of a method.

Official example:

Def setup_func ():

"set up test fixtures"

Def teardown_func ():

"tear down test fixtures"

@ with_setup (setup_func, teardown_func)

Def test ():

"test..." five。 Doctest

The doctest module searches for Python code snippets that look like interactive sessions, and then attempts to execute and validate the results.

In doctest, if you want to write a test case, you only need to write it in a document comment surrounded by', where it can be referenced by the doc attribute. This is special and different from other unit testing frameworks. But I think this means that doctest is not suitable for large-scale testing, because there is no separation between code and testing.

Import doctest

"

This is the "example" module.

The example module supplies one function, factorial (). For example

> > factorial (5)

one hundred and twenty

"

Def factorial (n):

"" Return the factorial of n, an exact integer > = 0.

> [factorial (n) for n in range (6)]

[1, 1, 2, 6, 24, 120]

> > factorial (30)

265252859812191058636308480000000

> factorial (- 1)

Traceback (most recent call last):

...

ValueError: n must be > = 0

Factorials of floats are OK, but the float must be an exact integer:

> factorial (30.1)

Traceback (most recent call last):

...

ValueError: n must be exact integer

> > factorial (30.0)

265252859812191058636308480000000

It must also not be ridiculously large:

> factorial (1e100)

Traceback (most recent call last):

...

OverflowError: n too large

"

Import math

If not n > = 0:

Raise ValueError ("n must be > = 0")

If math.floor (n)! = n

Raise ValueError ("n must be exact integer")

If nasty 1 = = n: # catch a value like 1e300

Raise OverflowError ("n too large")

Result = 1

Factor = 2

While factor = 0

Ok

Trying:

Factorial (30.1)

Expecting:

Traceback (most recent call last):

...

ValueError: n must be exact integer

Ok

Trying:

Factorial (30.0)

Expecting:

265252859812191058636308480000000

Ok

Trying:

Factorial (1e100)

Expecting:

Traceback (most recent call last):

...

OverflowError: n too large

Ok

1 items had no tests:

_ _ main__

1 items passed all tests:

6 tests in__ main__.factorial

6 tests in 2 items.

6 passed and 0 failed.

Test passed. After reading this, the article "what are the five common testing frameworks for python" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, please follow the industry information channel.

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

Development

Wechat

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

12
Report