Python test Module--nose

Source: Internet
Author: User

The following is translated from nose official documents, because the level is limited, inevitably there will be errors ....


Nose is a test tool for the Python platform, and it is believed that people who have developed it with Python know that Unittest,nose is more powerful and more useful than unittest (note: I have not used unittest ... I'm still a little white .... )

The tests used by nose is unittest. TestCase subclasses, of course, you can also write some simple test functions and test classes, and not necessarily if the unittest. The subclass of the testcase.

It's easy to run tests using nose, just follow simple rules to organize your libraries and code, set up a test environment is simple, nose have a lot of built-in plugins to help you capture output, error messages, and so on, of course, if you don't like the default collection options, you can write the plugin yourself.


Basic methods of Use:

nosetests [Options] [(optional) test files or directories]

or write a. Noserc configuration file (or create a new setup.cfg file under the project directory) in your home directory:

[Nosetests]verbosity=3with-doctest=1

If you want to disable the configuration file, set an environment variable parameter: Nose_ignore_config_files.


Nose automatically collects tests from Python source files, directories, and packages, any matching regular expressions (the default is: (?: ^| [b_.-]) [Tt]est] source files, directories and packages will be collected for testing. The package will follow the "tree" down to look for conditions to be used for testing. In a test directory or package, any Python source file that matches Testmatch is checked for use as a test example. In a module, the names matching testmatch functions and subclasses of classes and testcase are used as tests. Tests will produce a asserterror to show the error. Here's a place to note:nose by default, files that have executable permissions are not tested . To test such a file either remove the ' x ' bit, or use the-exec option.


nosetests filename or module

The working directory of Nosetests can be represented directly with the directory after nosetests.


Using plugins: nosetests--plugins: Displays installed plugins,-V,-VV displays the details of the plugin.

For more information on the nosetests options, please see the official website ...


Test Package:nose allows tests to be put into a test package, which is tested in packages. Each test run can be created in the package or removed from the package without the need for a separate test run on the test module or the test example tests. In order to implement this "package-level" test method, the setup and teardown functions (which are simply run before the test, which are run after the test) are put into the __init__.py file. The Setup method can be named Setup, Setup_package, Setup, or Setuppackage;teardown methods can be named Teardown, Teardown_package, teardown or Teardownpackage. Tests are started as soon as the first Test module is loaded into the test package.


The test Module:test module is a block that matches the Testmatch regular expression, which can be named Setup, Setup_module, Setup or Setupmodule for setup, teardown, Teardown_module,
or Teardownmodule for teardown. Search tests in a test module and then perform the test.


Test Class:test class is a class or subclass of Unittest.testcase that matches the Testmatch regular expression in the test module. All test classes run the same way: After the method that matches the Testmatch regular expression is found, each method is tested by creating a new instance of the Test class. Class-level adorners can be named Setup_class, SetupClass, SetupClass, Setupall or Setupall; Teardown fixtures May
Named Teardown_class, Teardownclass, Teardownclass, Teardownall or Teardownall.


Test function: Any functions in a test module are packaged as a functiontestcase and then run the test. The test function can define the Setup/teardown property. If there are multiple functions in the same test module that require the same setup/teardown, you can use the With_setup adorner.

Def setup_func (): .... def teardown_func (): ... @with_setup (SETUP_FUNC,TEARDOWN_FUNC) def test (): "Test ..." Note: with_ Setup can only be used for functions, not for test method in test class. In those cases, define the setup and Teardown methods.

Test Generator:setup and teardown can be used with generator functions, but it is important to note that if Setup and teardown are attached to a generator function, they will only be executed once, This means that they will not be executed at a later time when the generator continues to run, and in order to test the value of each yield, it can be appended to the yield function.


More about nose plug-in details and source code please crossing Web documents ....

This article from "Lotus's Thoughts" blog, please be sure to keep this source http://liandesinian.blog.51cto.com/7737219/1562770

Python test Module--nose

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.