Tutorial by: Olyno


Introducing to Unit tests with Skester

Skester banner

Overview

1) Setup
2) What's a "Unit" test?
3) Why create unit tests with Skester?
4) My first test
5) Dynamic test
6) Go further
7) FAQ

Setup

1) Download Skester
2) Place Skester inside your plugins directory
3) Reload your server

What's a "Unit" test?

In computer programming, unit testing is a software testing method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use.

Source: Wikipedia

So to explain it simply, it able you to test parts of code.

Why create unit tests with Skester?

My first test

Imagine, we create a game. We want to be sure that the player is teleported to a special location.

command /startgame:
    trigger:
        it "should teleport players correctly":
            teleport player to {game::my_game::teams::red::spawn}
            assert equals location of player to {game::my_game::teams::red::spawn}

Here, if the player's location is not the red team's location, it will throw an error. Sounds good right?

Now, I want to know if the player got items he needs.

command /startgame:
    trigger:
        it "should teleport players correctly":
            teleport player to {game::my_game::teams::red::spawn}
            give diamond sword to player
            assert equals location of player to {game::my_game::teams::red::spawn}
            assert if inventory of player contains diamond sword

This test works and it is cool. But if someone read your code, it will not understand everything,same if you get an error from this test. So, let's add messages to this errors.

command /startgame:
    trigger:
        it "should teleport players correctly":
            teleport player to {game::my_game::teams::red::spawn}
            give diamond sword to player
            assert equals location of player to {game::my_game::teams::red::spawn} with message "Player should be teleported to the red spawn"
            assert if inventory of player contains diamond sword with message "Player should have a diamond sword in its inventory"

Dynamic test

Now let's see how we could make dynamic tests. I have players inside each teams, and I want like the previous test, check if players are teleported with success, and everyone got a diamond sword.

command /startgame:
    trigger:
        it "should teleport players correctly":
            loop "blue" and "red":
                loop {game::my_game::teams::%loop-value%::players::*}:
                    teleport loop-value-2 to {game::my_game::teams::%loop-value-1%::spawn}
                    give diamond sword to loop-value-2
                    assert equals location of loop-value-2 to {game::my_game::teams::%loop-value-1%::spawn} with message "%loop-value-2% should be teleported to the %loop-value-1% spawn"
                    assert if inventory of loop-value-2 contains diamond sword with message "%loop-value-2% should have a diamond sword in its inventory"

Our test is now dynamic!

Go further

This post was just a introduction to unit tests with Skester. I invite everyone to make these tests.

Here is the documentation with all syntaxes available with Skester:

SkriptHubViewTheDocs

FAQ

I use broadcast effect to debug and it works fine, why should I use Skester

Broadcast was a simple way to make tests. But a broadcast will just show a message. Never you will get any informations of what you are waiting and what you got.
Sometimes you add a new feature, and when you try your old code, it doesn't work. So you use broadcast to solve it, you continue to dev your script, and again you retry your old code and it doesn't work again. Skester will save you this time by creating only a test at the beginning and know what is wrong and when.


Did you find Olyno's tutorial helpful?


You must be logged in to comment