Python Nose test Framework Overview Three

Source: Internet
Author: User

Three, nose test tool Set

The Nose.tools module provides a range of widgets, including test execution time, abnormal output, and all assert functions in the unittest framework.

In order to make writing use cases easier, Nose.tools provides some handy functional functions, which are written in several common, as follows:

Nose.tools.ok_ (expr, Msg=none)

The standard assert, examples are as follows:

 from Import eq_ def test_lean_2 ():     Print " test_learn_2 "     ok_ (4==3,msg="Error")

The results of the operation are as follows:

E:\workspace\nosetest_lear\test_case>nosetests-v test_case_0001:test_lean_2test_case_0001.test_lean_2 ... FAIL======================================================================FAIL:test_case_0001.test_lean_2----------------------------------------------------------------------Traceback (most recent): File"c:\python27\lib\site-packages\nose\case.py", line 197,inchruntest Self.test (*self.arg) File"E:\workspace\nosetest_lear\test_case\test_case_0001.py", line 26,inchtest_lean_2 Ok_ (4==3,msg="XXX") assertionerror:xxx-------------------->> begin captured stdout <<---------------------test_learn_2--------------------->> End captured stdout <<-------------------------------------------------------- ------------------------------------Ran1 Testinch0.045sFAILED (Failures=1) E:\workspace\nosetest_lear\test_case>

Nose.tools.eq_ (A, B, Msg=none)

Quick comparison of parameters A to B

 from Import eq_ def test_learn_1 ():    eq_ (5, 6, msg="wrong")

The results of the operation are as follows:

======================================================================FAIL:test_case_0001.test_learn_1----------------------------------------------------------------------Traceback (most recent): File"c:\python27\lib\site-packages\nose\case.py", line 197,inchruntest Self.test (*self.arg) File"E:\workspace\nosetest_lear\test_case\test_case_0001.py", line 20,inchtest_learn_1 Eq_ (5, 6, msg="wrong") Assertionerror:wrong

Nose.tools.assert_in (Member, container, Msg=none)

The code is as follows:

 from Import assert_in def test_lean_5 ():    assert_in ("aaa",'bbb' , msg="test in  failed")

The results of the operation are as follows:

======================================================================FAIL:test_case_0002.test_lean_5----------------------------------------------------------------------Traceback (most recent): File"c:\python27\lib\site-packages\nose\case.py", line 197,inchruntest Self.test (*self.arg) File"E:\workspace\nosetest_lear\test_case\test_case_0002.py", line 24,inchtest_lean_5 assert_in ("AAA",'BBB', msg="Test in failed") Assertionerror:testinchfailed----------------------------------------------------------------------

Other assert here does not say, Nose.tools contains a lot of content, can directly query.

Here are a few more useful nose.tools tools

Nose.tools.set_trace ()

-------------Single-Step debugging tool, this feature works well in multiple modules and large programs. Pdb.set_trace is used internally.

The code is as follows:

 from Import assert_in  from Import Set_trace def test_lean_5 ():    set_trace ()    assert_in("aaa",'  BBB', msg="test in  failed")

The results are as follows:

Test_case_0002.test_lean_5 ... >E:\workspace\nosetest_lear\test_case\test_case_0002.py (26) test_lean_5 ()-Assert_in ("AAA",'BBB', msg="Test in failed") (Pdb) nassertionerror:assertio...failed',)> e:\workspace\nosetest_lear\test_case\test_case_0002.py (26) test_lean_5 ()-Assert_in ("AAA",'BBB', msg="Test in failed") (PDB) n--return--> e:\workspace\nosetest_lear\test_case\test_case_0002.py (+) test_lean_5 ()None-Assert_in ("AAA",'BBB', msg="Test in failed") (Pdb) nassertionerror:assertio...failed',)> c:\python27\lib\site-packages\nose\case.py (197) runtest ()Self.test (*Self.arg) (Pdb) Cfail0002Test teardown======================================================================FAIL:test_case_0002.test_lean_5----------------------------------------------------------------------Traceback (most recent): File"c:\python27\lib\site-packages\nose\case.py", line 197,inchruntest Self.test (*self.arg) File"E:\workspace\nosetest_lear\test_case\test_case_0002.py", line 26,inchtest_lean_5 assert_in ("AAA",'BBB', msg="Test in failed") Assertionerror:testinchFailed

Specific PDB debugging methods can be found in http://www.cnblogs.com/chencheng/archive/2013/07/07/3161778.html

Nose.tools.timed (limit)

The test must be completed within the set time (in seconds) or the test will fail; The code is as follows:

 from Import timed Import time@timed (1)     def test_lean_5 ():    time.sleep (2)    Pass

The test results are as follows:

test_case_0002.test_lean_5 ... FAIL======================================================================FAIL:test_case_0002.test_lean_5----------------------------------------------------------------------Traceback (most recent): File"c:\python27\lib\site-packages\nose\case.py", line 197,inchruntest Self.test (*self.arg) File"c:\python27\lib\site-packages\nose\tools\nontrivial.py", Line 100,inchNewfuncRaiseTimeExpired ("Time limit (%s) exceeded"%limit) Timeexpired:time Limit (1) exceeded----------------------------------------------------------------------Ran1 Testinch2. 006sFAILED (Failures=1)

There is also a set of test cases in the nose.tools is to be measured and contingency options, here is not introduced.

The following section describes the use of the built-in plug-in for nose

Python Nose test Framework Overview Three

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.