In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what are the common testing frameworks of Python". In daily operation, I believe many people have doubts about the common testing frameworks of Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the common testing frameworks of Python?" Next, please follow the editor to study!
Common rules for testing
A test unit must focus on a small function to prove that it is correct.
Each test unit must be completely independent and must be able to run separately. This means that each test method must reload the data and do some cleaning after execution. Usually handled by the setUp () and setDown () methods
Write test code that performs quickly. In some cases, tests need to load complex data structures and reload each time they are executed, when the test execution is slow. Therefore, in this case, you can place this test in a background task.
Use the test tool and learn how to use it.
Perform the full test before you write the code, and do it again after you write the code. This ensures that the code you write later won't break anything.
Perform a complete test before submitting the code
If your work is interrupted during development, write an interrupted unit test about what you will develop next. When you come back to work, you will know the pointer developed in the previous step.
Unit test functions use long and descriptive names. In formal execution code, you may use the name square () or sqr (), but in the test function, you must use names like test_square_of_number_2 () and test_square_negativer_number (), which are more clearly described
The test code must be readable
Unit testing is a working guide for new developers.
The purpose of unit testing is to check the correctness of a module, a function or a class. If the unit test passes, it shows that the object we are testing can work properly. If the unit test fails, either the test object has bug or the test condition is entered incorrectly. The following editor introduces several testing frameworks of Python.
1. 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.
Example:
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 ()
2. 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.
3. Pytest
Reference document: http://pytest.org/latest/
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.
Example:
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.
Example:
Def setup_func ():
"set up test fixtures"
Def teardown_func ():
"tear down test fixtures"
@ with_setup (setup_func, teardown_func)
Def test ():
"test..."
5. 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', that is, where you 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. At this point, the study on "what are the common testing frameworks for Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.