Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create and run a basic Python test program

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to create and run a basic Python test program, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

1. Theme

Here we focus on how Pycharm helps us create and run a basic test program. As for how to write specific test programs, see the previous article.

2. Preparatory work

Make sure you have the Python interpreter installed on your computer. Versions 2.4 to 3.3 are fine.

3. Create a simple Python project

In the main menu, select File | New Project

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

Click OK and select the window needed to display the project. Here we select the first option-- to open our project in a separate new window:

4. Create a Python class

Press Alt+Insert and select Python file:

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

You can see that the _ _ author _ _ and _ _ project__ variables have been defined in the newly created Python file, so let's create a simple script to solve the quadratic equation:

5. Create a test program

Right-click the class name and select Go to | Test from the shortcut menu (you can also press Ctrl+Shift+T directly):

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

The results are as follows:

As you can see, the test program created meets the Python unit testing framework standard-- import the test class for the response from the unittest module and prefix the test function name with "test".

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

It is recommended to use the spelling prompt function when entering. Through the Ctrl+Space key combination, Pycharm will give the appropriate module and class name prompt:

If the statement declared by import is grayed out, the currently imported module has not been used.

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

The final code of the test unit is as follows:

Our test scenario consists of two test methods: test_negative_discr and test_demo, which is not usually executed.

Notice that the import statement is no longer grayed out because we have used the Solver class in the test_negative_discr function.

6. Other methods for creating test scenarios

Let's try another way to create a test scenario. Press the Alt+Insert key combination, select Python file from the pop-up menu, then select the Python unit test option in the New Python file dialog box Kind, and type the name of the test scenario:

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

To execute our test unit, Pycharm recommends using a new configuration file, run/debug configuration, which is predefined and can be used directly. Press Ctrl+Shift+F10, or right-click in the class and select Run unittests in test_solver:

The running results are as follows:

On how to create and run a basic Python test program to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report