Let's statically check and format the code of E2E automatic test written in Python [VS Code]

background

I think that the person who writes the code of the product usually has the format & static check at the time of saving on the editor side during development.

On the other hand, testers and test engineers will challenge the automated test with GUI operation! I tend to get out of this when it becomes, so I tried to wake it up in the article after the introduction.

What you can do with reference to this article

When writing automated test code, every time I save

--It automatically formats (= formats) the shape according to certain rules, such as the presence or absence of blank lines and spaces, and line breaks on lines that are too long. ――It points out redundant writing styles and points out unused modules / variables.

The state can be realized.

As a result, when multiple people are coding the automated test, the appearance of each automated test code is unified to some extent & unnecessary lines are reduced, making it easier to read and write.

This time, we will set it so that it (basically) adheres to the Python coding standard PEP8.

environment

procedure

1. Install what you need

Execute the following command to install flake8 and autopep8.

> pip install flake8
> pip install autopep8

2. VS Code settings

Add the following to the existing settings described in settings.json. Or, if the item already exists, overwrite it.

{
    "editor.formatOnSave": true,
    "python.linting.pylintEnabled": false,
    "python.linting.enabled": true,
    "python.linting.flake8Enabled": true,
    "python.linting.flake8Args": [
        "--ignore=E501"
    ],
    "python.formatting.autopep8Args": [
        "--max-line-length=200",
    ]
}

--ʻEditor.formatOnSave: Whether to format automatically when saving a file --python.linting.pylintEnabled: Whether to use pylint --python.linting.enabled: Whether to lint --python.linting.flake8Args: Parameters to pass to flake8. This time --ignore = E501 avoids E501's" line is too long "pointing out --python.formatting.autopep8Args: Parameters to pass to autopep8. This time --max-line-length = 200` sets the line feed limit to 200 characters

If you can do so far, after that, when you save the code with VS Code, it will automatically format and point out problems.

3. See the indication

At the bottom of the screen, in the "Problem" tab, the pointed out content is displayed.

flake8-and-autopep8.PNG

If it does not appear, please click on the x or △ mark at the bottom left.

flake8-and-autopep8_2.PNG

Patterns that are troublesome with automatic test code for system testing

If you can write automated test code with the same rules as the product development team, it is convenient because more people can maintain automated tests.

However, there are some situations where it is difficult to write automated test code according to the product coding standards.

For example, the limit on the number of characters in one line. At the time of development, if it is too long, it is required to insert an appropriate line break. In autopep8 used this time, line breaks are automatically set to fit in 79 characters by default.

However, when I'm writing an automated test in Selenium and I have to specify a long xpath, it's easier to see if it's stretched horizontally rather than a strange line break.

Therefore, this time, the limit on the number of characters in a line has been changed to 200. Please adjust here to your liking.

reference

-[Introduce flake8 instead of pylint in VS Code Python development environment and set automatic formatting --Qiita](https://qiita.com/psychoroid/items/2c2acc06c900d2c0c8cb#%E3%83%95%E3%82% A1% E3% 82% A4% E3% 83% AB% E4% BF% 9D% E5% AD% 98% E6% 99% 82% E3% 81% ABlinting% E3% 82% 92% E3% 83% 81% E3% 82% A7% E3% 83% 83% E3% 82% AF% E3% 81% 99% E3% 82% 8B% E8% A8% AD% E5% AE% 9A% E3% 81% AB% E3% 81% 99% E3% 82% 8B) -Comfortably follow VS Code coding conventions --Qiita

Recommended Posts

Let's statically check and format the code of E2E automatic test written in Python [VS Code]
Let's measure the test coverage of pushed python code on GitHub.
Check the behavior of destructor in Python
I want to format and check Python code to my liking on VS Code
I want to write in Python! (1) Code format check
Let's break down the basics of TensorFlow Python code
Let's use the open data of "Mamebus" in Python
Check the asymptotic nature of the probability distribution in Python
[Machine learning] "Abnormality detection and change detection" Let's draw the figure of Chapter 1 in Python.
Check the processing time and the number of calls for each process in python (cProfile)
Test code to check for broken links in the page
Check the operation of Python for .NET in each environment
Comparison of exponential moving average (EMA) code written in Python
I tried programming the chi-square test in Python and Java.
The process of making Python code object-oriented and improving it
[Tips] Problems and solutions in the development of python + kivy
Determine the date and time format in Python and convert to Unixtime
I compared the calculation time of the moving average written in Python
[Python] Let's reduce the number of elements in the result in set operations
How to check the memory size of a variable in Python
I wrote the code to write the code of Brainf * ck in python
How to check the memory size of a dictionary in Python
Let's summarize the degree of coupling between modules with Python code
Get the title and delivery date of Yahoo! News in Python
The story of Python and the story of NaN
Automatically format Python code in Vim
Check and move directories in Python
Write selenium test code in python
Set up a dummy SMTP server in Python and check the operation of sending from Action Mailer
Analyze the source code of your own simple search engine written in Python with the code visualization tool "SOURCE TRAIL"
Comparing the basic grammar of Python and Go in an easy-to-understand manner
Test & Debug Tips: Create a file of the specified size in Python
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
Python code to train and test with Custom Vision of Cognitive Service
Open an Excel file in Python and color the map of Japan
I made a program to check the size of a file in Python
Code reading of faker, a library that generates test data in Python
Stress Test with Locust written in Python
Write the test in a python docstring
Check and receive Serial port in Python (Port check)
Check if the URL exists in Python
[Python3] Rewrite the code object of the function
Check the path of the Python imported module
The basics of running NoxPlayer in Python
Find and check inverse matrix in Python
In search of the fastest FizzBuzz in Python
[Python] Get the character code of the file
Get the EDINET code list in Python
Project Euler # 1 "Multiples of 3 and 5" in Python
python setup.py test the code using multiprocess
Create a Python image in Django without a dummy image file and test the image upload
How to make VS Code aware of the venv environment and its benefits
plot the coordinates of the processing (python) list and specify the number of times in draw ()
I compared the speed of regular expressions in Ruby, Python, and Perl (2013 version)
Let's automatically display the lyrics of the song being played on iTunes in Python
Let's play with Python Receive and save / display the text of the input form
Divides the character string by the specified number of characters. In Ruby and Python.
[AWS] Let's run a unit test of Lambda function in the local environment
List of main probability distributions used in machine learning and statistics and code in python
Get the last element of the array by splitting the string in Python and PHP
Output the number of CPU cores in Python