Another option for python unit testing

Get into the habit of writing together! This is the 13th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the event details .

If you have learned python for automated testing, you must have used unittest. Today we are going to talk about nose2 is an advanced version of unittest. It is easier to understand than unittest and more convenient to use.

One, a quick start

nose2 is developed on the basis of unittest, so if you use unittest for testing before, but want to use the new features of nose2, you can use nose2 directly without changing the original code.

  
import unittest
import nose2

class TestAdd(unittest.TestCase):
    def test_add(self):
        self.assertEqual(1+1, 3)

if __name__ == '__main__':
    # 只需要替换这一行
    # unittest.main()
    nose2.main()
复制代码

Second, the installation

nose2 is a third-party library, you need to install it yourself, run the pip command directly in cmd to install:

  
pip install nose2
复制代码

Third, run the test case

In the quick start, we used nose2.main() in the python code to run the test cases, which is perfectly fine.

There is another way to run it: run it directly from the command line through the nose2 command. It looks in the python files for test files whose names start with test, and runs every test function name it finds that starts with test. For example, there are files such as test_add.py and test_minus.py in my project. If I want to run all the test cases, I just need to enter the cmd command in the current file, and it will automatically find all the test cases and execute them. :

  
nose2 
复制代码

1. Run the test cases in the specified folder

  
nose2 -s 文件夹名称
复制代码

I put all login-related use cases into a folder called login_case. When I nose2 -s login_caseexecute , other use cases will not be executed, and nose2 will only find the use cases under the login_case folder.
image.png

2. Run the specified test case

  
nose2 login_case.test_nose2_dir.test_login
复制代码

In this example:

  • login_case is the folder where it is stored
  • test_nose2_dir is the file name
  • test_login is the name of the test case method

Each level is separated by a .sign .

If you want to test all use cases under test_nose2_dir.py file:

  
nose2 login_case.test_nose2_dir
复制代码

If you want to test all use cases under the login_case folder:

  
nose2 login_case
复制代码

Fourth, the naming of test cases

Why can nose2 automatically find use cases and execute them? It's because he made the rules internally, and then judged according to the rules.

For example, first of all he stipulates that all your test case file names should start with test. If you don't do this, then this file is not a test case file. The following file names will be identified as test case files:

  
test.py
test_add.py
testRegister.py
复制代码

And these will not be judged as test case files and will be ignored:

  
add_test.py
a_test.py
register_test_file.py
复制代码

V. Summary

In this article, we introduce a simple introduction to nose2:

  • nose2 is implemented on the basis of unittest and is compatible with unittest. If you have not been exposed to unittest, it is recommended to start with unittest. Many students start to criticize when they see some remarks on the Internet saying that unittest is outdated or that unittest is not advanced. ,wrong. unittest is the foundation of other python unit testing frameworks. If you don't learn unittest well, you can't learn other frameworks well.
  • nose2 can automatically discover test cases, you just need to run the nose2 command, it is very simple to use.
  • The test case name of nose2 must conform to the specification and start with test, otherwise it will not be judged as a test case.
  • You can -srun the use case under the specified file through , or you can .run a single use case through the number.

Guess you like

Origin juejin.im/post/7086458428620537887