Skip to content

Testing solutions written in different languages

Sergey edited this page May 25, 2023 · 10 revisions

Table of contents

  1. JVM languages
    1. Java, Kotlin, Scala
  2. Non-JVM languages
    1. Initial setup
    2. Python
      1. Project structure
      2. Enable code completion
      3. File templates
    3. Go
      1. Project structure
      2. File templates
    4. JavaScript
      1. Project structure
      2. File templates
    5. Bash
      1. Project structure
      2. File templates

The hs-test library supports testing solutions written in different languages. Let's discuss how to create them.

JVM languages

Java, Kotlin, Scala

The main language for checking solutions with the hs-test library is JVM languages. Java, Kotlin and Scala are currently supported among them.

In this case, the project structure and initial templates are described in file templates section.

Non-JVM languages

Initial setup

When creating a project with a solution intended to be in a non-JVM language, you still need to create the project in JetBrains Academy using Type: Hyperskill Java. The contents of Description will not be shown anywhere, so don't worry about that.

The reason you have to choose Type: Hyperskill Java is that in this case JetBrains Academy plugin upon clicking Check will run Java/Kotlin tests locally (because it knows that it totally can check Java and Kotlin projects locally). The hs-test however, will see no files with extension .java, .koltin or .scala and will run other files based on the file extension.

After creating a JetBrains Academy project, you need to change build.gradle and settings.gradle according to the tests template and following sections. This will ensure that Java/Kotlin tests will run correctly.

Important: At the moment, it is impossible to open Cogniterra lessons in IDE with tests written in Java/Kotlin and with a solution in non-JVM language. So, do not delete the folder with a project after you have uploaded in to Cogniterra. You should use this folder to apply subsequent changes to the tests and descriptions when you wish to do such changes. If you delete such folder, you will no longer be able to update tests in the project. For the support opening such Cogniterra lessons in JetBrains Academy see issue EDU-4878.

Python

Python project structure

In case of Python, you don't need src folder - you can just place one or multiple .py files right in the root of the stage. It will be the initial template for the project. You can also create various deeper folders, for example data and place some Python files there. They can be imported using standard Python's importing process: import data.file1.

If you want to install some additional dependencies that users should be able to use in their solution, you should create requirements.txt in the root of the project, in the same folder where build.gradle is located. But just remember that with additional dependencies users will not be able to solve the project in a web-interface on Hyperskill.

After creating tests and Python solution, you can test it using the button Check in the Task section of JetBrains Academy plugin. Additionally, you can open the tests and click on the green triangle to run tests explicitly without JetBrains Academy plugin.

Note: To run tests properly, you need the command python to be executable in the command line (in your path). hs-test will run exactly this command to run the user's solution.

You should end up with the following file project structure:

Enable code completion for Python

First time, you will see No Python interpreter configured for this module error. For Python code completion to work, do the following steps:

You need to click on stage1 -> Add Framework Support...

Then click Python.

You can choose already existing Python interpreter. However, if you want to use additional dependencies that are described in requirements.txt you should create virtual environment. For this, click on ... -> + (Add new SDK) -> Add Python SDK and create virtual environment.

Python file templates

Usually, the initial template for Python projects is just one line:

print('Hello, Wolrd!')

Go

Go project structure

In case of Go, you don't need src folder - you can just place .go files right in the root of the stage. It will be the initial template for the project.

After creating tests and Go solution, you can test it using the button Check in the Task section of JetBrains Academy plugin. Additionally, you can open the tests and click on the green triangle to run tests explicitly without JetBrains Academy plugin.

Note: To run tests properly, you need the command go to be executable in the command line (in your path). hs-test will run exactly this command to run the user's solution.

After that, you should end up with the following file structure:

Go file templates

You can create the initial file with the following content:

package main

func main() {
    // write your code here
}

JavaScript

JavaScript project structure

In case of JavaScript, you don't need src folder - you can just place one or multiple .js files right in the root of the stage. It will be the initial template for the project.

You should create dependencies file named package.json in the root of the project with the following content:

{
  "devDependencies": {
    "sync-input": "https://github.com/hyperskill/sync-input/archive/release.tar.gz"
  }
}

The reason for this dependency is that NodeJS doesn't have input() command like Python or System.in like Java, so this dependency adds support for such functionality. There's separate repository for this dependency, it's tiny dependency.

You can add additional dependencies here if you want the users to be able to use them in their solution, just remember that with additional dependencies users will not be able to solve the project in a web-interface on Hyperskill since only sync-input is installed there.

After creating this file, IDEA will suggest you to run npm install, you should do that.

After creating tests and JavaScrint solution, you can test it using the button Check in the Task section of JetBrains Academy plugin. Additionally, you can open the tests and click on the green triangle to run tests explicitly without JetBrains Academy plugin.

Note: To run tests properly, you need the command node to be executable in the command line (in your path). hs-test will run exactly this command to run the user's solution.

After all of that, you should end up with the following file structure:

JavaScript file templates

You can create the initial file with the following content. Since sync-input is an additional dependency, you can leave the comment explaining what it's doing and how to use it. You can remove the line // You will need this in the following stages if the user is required to input some data already in the first stage.

// Use "input()" to input a line from the user
// Use "input(str)" to print some text before requesting input
// You will need this in the following stages
const input = require('sync-input')

console.log("Hello, World!")

Bash

Bash project structure

In case of Bash, you don't need src folder - you can just place .sh files right in the root of the stage. It will be the initial template for the project.

After creating tests and Bash solution, you can test it using the button Check in the Task section of JetBrains Academy plugin. Additionally, you can open the tests and click on the green triangle to run tests explicitly without JetBrains Academy plugin.

Note: To run tests properly, you need the command bash to be executable in the command line (in your path). hs-test will run exactly this command to run the user's solution. If you're on Windows, you need WSL2 to be installed. However, users that will try to solve the project don't need to meet such requirements because when completing such Bash project on Hyperskill, JetBrains Academy will send the solution to be checked on Hyperskill side. But for development, you need to have bash locally.

After that, you should end up with the following file structure:

Bash file templates

You can create the initial file with the following content:

echo Hello, World!