Most complete Pycharm Tutorials (9)--Create and run a basic Python test program

Source: Internet
Author: User

Most complete Pycharm Tutorials (1)--Custom Skins

Most full Pycharm Tutorials (2)-code style

Most full Pycharm Tutorials (3)-code debugging, running

Most complete Pycharm Tutorials (4)--related configuration for Python interpreter

Most full Pycharm Tutorials (5)--python shortcut key Related settings

Most full Pycharm Tutorials (6)--using Pycharm as a VIM editor

Most full Pycharm Tutorials (7)--Configuration of VM VMs

Most complete Pycharm tutorials (8) creation and management of--django engineering

1. Theme

Here we highlight how Pycharm helps us create and run a basic test program. As for how to write a specific test procedure, refer to the previous article.

2. Preparatory work

Verify that the Python interpreter is already installed on your computer and that versions 2.4 through 3.3 are available.

3. Create a simple Python project

From the main menu, select File | New Project

In the Create Project dialog box, enter the project name (tentatively testsamples here), select the project type (choose an empty project here), and specify the Python interpreter version:

Click OK, select Show Project window, here we choose the first option--open our project in a separate new window:

4. Create a Python class

Press Alt+insert to select Python file:

In the New Python dialog box, enter the file name:

You can see that the __author __ and the __project__ variables are already defined in the new Python file, and then we create a simple script that implements the function of solving the two-second equation:

5. Create a test program

Right-click the class name and choose Go to | from the shortcut menu Test(You can also press ctrl+shift+tdirectly):

In the Create Test dialog box, enter the path and name, tick the test_demo function option in the check box:

The results are as follows:

As you can see, the test program that you create satisfies the Python unit Testing framework standard-the test class that imports the response from the UnitTest module and adds the test function name to the "test" prefix.

However, the current test unit is just a basic framework that needs to be modified. First, import the relevant modules:

It is recommended to use the spelling hint function when inputting, Pycharm will give the appropriate module and class name hint through ctrl+space combination key:

If the statement for import declaration is grayed out, the currently imported module is not yet in use.

Next we create a function to throw an exception with a negative discriminant, and add the following code to the test class:

The final code for the test unit is as follows:

Our test scenario consists of two test methods: TEST_NEGATIVE_DISCR and Test_demo, although the latter is usually not executed.

Note that the import statement at this point is no longer grayed out because we have used the Solver class in the TEST_NEGATIVE_DISCR function.

6. Other ways to create test scenarios

Let's try another method to create a test scenario. Press Alt+insert to select python filein the pop-up menu, then select the Python Unit Test option in the New Python file dialog kind and type the test scheme name:

At this point pycharm creates and initializes a solution program, opens and edits it:

Similar to the previous steps, the final Test code is:

7. Run the test unit

In order to execute our test unit, Pycharm recommends using a new configuration file Run/debug configuration, which is already pre-defined and we can use it directly. Press CTRL+SHIFT+F10, or right-click inside the class and select Run unittests in Test_solver:

The results of the operation are as follows:

Full Pycharm Tutorial (9)--Create and run a basic Python test program

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.