Automatic sample test case & manual input value test execution with VS Code with AtCoder

Introduction

Until now, AtCoder was done in Python x Jupyter Lab. This time, I want to use VS Code newly, and I have had trouble executing sample test cases every time, so I will leave it because there was a way to try google!

The goal this time is to be able to perform the following operations.

  1. Automatically execute the test in the sample test case with Ctrl + Shift + B on VS Code
  2. Run a test with manually entered values with F5 on VS Code

environment

I'm building on Ubuntu this time, but since I'm using shell scripts and VS Code, it's okay if Windows Subsystem for Linux is included in Windows as well as Mac.

[Updated May 28, 2020]

There was an article that I practiced on Windows 10! Competition Pro with Python and VSCode-Simplification of standard input and automation of sample case judgment-

Referenced site

It feels like I was able to pick up and eat from each. It was very helpful. Thank you! Building a competitive professional environment with Visual Studio Code (practice) Create a Python environment for competitive professionals with VS Code on Windows [GitHub]online-judge-tools

Folder structure

I cut a directory called AtCoder and build the environment there. Contest questions will be pushed into the problems directory with the file name {contest name} \ _ {problem name} .py. Please note that the {contest name} part of the file name will not work unless it is the same as the URL of the AtCoder contest page.

//atcoder.jp/contests/{Contest name}


 File name example:
 AtCoder Beginner Contest 158 A Problem
 (URL:https://atcoder.jp/contests/abc158/tasks/abc158_a)
  →abc158_a.py
 Hitachi, Ltd. Social Systems Division Programming Contest 2020 C Problem
 (URL:https://atcoder.jp/contests/hitachi2020/tasks/hitachi2020_c)
  →hitachi2020_c.py

```shell

AtCoder
├── cptest.sh #This time the main shell script
│
├── input.txt #File to put the manually entered value of goal 2
│
├── problems #Directory for storing contest issues
│
├── test #Directory for storing contest sample cases
│
└── .vscode #Directory for storing VS Code configuration files
    ├── launch.json #Goal 2 configuration file
    └── tasks.json #Goal 1 configuration file

Install and log in to online-judge-tools

It's an insanely useful tool that scrapes the sample test cases in question from the AtCoder contest site and runs them automatically.

Install with pip.

bash



$ pip install online-judge-tools

After the installation is complete, you will need to log in with your AtCoder account to use the tool. Log in with the following command.

bash



$ oj login -u {username} -p {password} "https://atcoder.jp/"

You can check your login below.

bash



oj login --check "https://atcoder.jp/"

cptest.sh If you execute the problem file name without extension (.py) as an argument like cptest.sh abc158_a while logged in, the test in the sample test case will be executed automatically. It is a shell script that does. Assigns it to VS Code's Ctrl + Shift + B, so you don't have to type it into a terminal to run it.

cptest.sh



#!/bin/bash

problem_name=$1
test_dir=test/${problem_name}
base_url=${problem_name%_*}

# make test directory
if [ ! -e ${test_dir} ]; then
    oj dl -d test/${problem_name} https://atcoder.jp/contests/${base_url}/tasks/${problem_name}
fi

oj test -c "python3 problems/${problem_name}.py" -d test/${problem_name}

oj dl -d test~Download the information of the sample test case of the contest question in the test directory in the part ofoj test -c~Test using the test case downloaded in the section.

Settings in VS Code

input.txt It is a text file that contains the input values of the manual test execution. You can use it from the terminal in the form of python abc158_a.py <input.txt. Will be assigned to F5, so it will not be executed from the terminal.

input.txt


(Describe the content you want to pass as standard input)

task.json This is a configuration file for assigning the test execution in the sample test case to `Ctrl + Shift + B```. With the .py file open, press `Ctrl + Shift + Bto configure the build task and create task.json``` as follows.

task.json


{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "test_atcoder_sample",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "type": "shell",
            "command": "${workspaceFolder}/cptest.sh",
            "args": [
                "${fileBasenameNoExtension}"
        }
    ]
}

launch.json f5It is a configuration file for running the vs code debugger. If you press `` `F5``` while the .py file you want to debug is open, it will take the contents of input.txt as input and execute the .py file.

Launch.json can be displayed by pressing `Ctrl + Shift + D``` or `F5``` in VS Code to put it in debug mode and clicking the gear in the upper left. When setting for the first time, "Create a new launch.json file" will appear on the left side (it should be), so create a new one for the Python file.

launch.json


{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": [
                "<",
                "input.txt"
            ]
        }
    ]
}

Test run (example of abc158_a.py)

After completing the above settings, all you have to do is write and execute the code. Let's take A problem of ABC158 as an example.

[Goal 1] Test execution in sample test case

Below is the capture when `` `Ctrl + Shift + B``` is executed.

Screenshot from 2020-03-15 01-43-35.png All sample test cases are AC.

[Goal 2] Manual test execution

I will also carry out a manual test. Let's test with the input "AAA". After saving as "AAA" in input.txt, go back to abc158_a.py and press `` `F5```.

input.txt


AAA

The result will be returned as "No".

Finally

Of course, the test in the sample test case is automated, but it is also very comfortable to be able to set the input value in input.txt. This is especially useful when the input value is a list or the type is repeated * i * times. The online-judge-tools used this time seems to be able to automate submissions other than sample tests, so it may be good to try various things. Also, I made my debut with VS Code on this occasion, but it seems that there are many functions that I haven't mastered yet, so I would like to continue to devote myself to it! !!

Recommended Posts

Automatic sample test case & manual input value test execution with VS Code with AtCoder
Build a python execution environment with VS Code
Strengthen with code test ⑦
Strengthen with code test ⑨
Strengthen with code test ③
Strengthen with code test ⑤
Strengthen with code test ④
Strengthen with code test ②
Strengthen with code test ①
Strengthen with code test ⑧
Strengthen with code test ⑨
PyCharm test code automatic generation
Debug Python with VS Code
How to build Python and Jupyter execution environment with VS Code