---
description: >-
  Unit tests are automation tests that are prepared for when making changes to
  the application can increase awareness to the unexpected change of application
  behaviour and most importantly, prevent bug
---

# Unit Tests

## Briefing

In unity, unit tests separated into PlayMode and EditMode tests, PlayMode tests is capable of automate any test that required Monobehaviour features, it runs on as it name suggest, Play Mode while EditMode tests is run under Editor where Monobehaviour scripts cannot be tested without problem. In this case, PlayMode is perfect for testing GUI scripts while EditMode is much more suitable for testing game logic, as for this package. In order to open Unit Tests tab, named Test Runner in Unity, see the gif below.

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2FO1PKC8pCQgHtqDFrN4Du%2FUnity_lPJiOhSaI0.gif?alt=media&#x26;token=17094ef9-82d2-461e-b163-25b4bb9726e7" alt=""><figcaption></figcaption></figure>

## PlayMode Tests

Only one test is prepared for PlayMode, it is for testing PGN text file imported from elsewhere, to test if our chess engine can run them without problem. &#x20;

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2FS9QJLUZtcKeIm8xFAm5w%2FUnity_avK7MmmIja.png?alt=media&#x26;token=3919ddb8-2483-447f-9055-81dd773fa10a" alt=""><figcaption></figcaption></figure>

To run the test, double click the PGNGUITextFilesTests test highlighted in blue on the image above. The results are as below.

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2FpeyUO7cbDN554dqXY4l8%2FUnity_i4SMpJYVuf.png?alt=media&#x26;token=18fe97dc-febf-4515-a450-46bb4067f73d" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2F7oTZoRV6zsursDVaxuuW%2FUnity_aDP5cRFHum.png?alt=media&#x26;token=61041a88-7856-44db-a909-378b42f311bc" alt=""><figcaption></figcaption></figure>

### What does the test automation do on the background?

1\) It will start the scene ChessPGN in PlayMode.

2\) Change the PGN Game dropdown to index 1, which is Micheal Adams in this case.&#x20;

3\) Then it will hit start button where all of the text in PGN text file will be converted to game data.

4\) From there, it will exhaustively select each matches available in the game from the PGNMatchesDropdown UI, just like how we would change PGN matches from the scene. It will also hit the "<" button until no more moves left to be undone before moving on into next match.&#x20;

Note that all of the steps above will not be visible to the eyes but Unity Editor will freeze the screen just to perform the test in highest speed possible.

**\*\*There will be no any error provided if PGN text files passes our engine standard.**



### Customize PGNGUITextFilesTests to test newly imported PGN text files

Open the file named PGNGUITests. There are three functions being prepared on this test, only one of it can be run at one time. **It is recommended to test all newly imported PGN text file just once**, so that the PGN scene can be free from bug that might be caused by PGN text file. To add new PGN text file to the PGN Game dropdown see [ChessPGN](scenes-details/chesspgn) page.

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2FtaElIiwcNTDbM9dmEJxp%2FUnity_UqatroheJq.png?alt=media&#x26;token=952007ab-6d3d-4e7d-aee7-b49de66b629f" alt=""><figcaption><p>PGN Game dropdown</p></figcaption></figure>

1\) `PGNFullTest` - For testing all PGN text files that's selectable through PGN Game dropdown(image above) in ChessPGN scene, in bulk. This test is not recommended as it will freeze the screen for so long if there's too many total matches to be tested, and it also repeat the tests for all dropdown that had been previously tested. Unless all of the dropdown hasn't been tested previously, otherwise use 2) function.

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2FDHkNRUXVRI2BykprWIot%2Fdevenv_o5BvBnAr1e.png?alt=media&#x26;token=0c684cc9-af3b-4cf4-8629-f212d29a635d" alt=""><figcaption></figcaption></figure>

2\) \*\***`PGNMatchesTest(Default)`** - For testing one full PGN text file, the pgnDropdownIndex variable in this function can be changed. The integer value is based on the PGN Game dropdown index. Like 1 is Micheal Adams and 2 is Magnus Carlsen. **This is the recommended function to be used at all time.**

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2Fep1axYQGjOYKpafTb4QI%2Fdevenv_ygVCgtHz0J.png?alt=media&#x26;token=f0e88f63-e008-404d-8aa0-33696d34e058" alt=""><figcaption></figcaption></figure>

3\) `PGNSingleMatchTest` - For testing one single match from a specific PGN text file. It is used only once error had been identified from a specific match. Once error occurs while running other test functions, we can see from the debug log which match number it stops at +1, there's where the problematic match number can be identified and be used in matchTest variable.

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2FQ10BIQ8oYAJr4J6dSdly%2Fdevenv_QZz8YkDfPL.png?alt=media&#x26;token=974e8fa1-8ea3-46ce-bf82-06e95847925d" alt=""><figcaption><p>PGNGUITextFilesTests function</p></figcaption></figure>

To switch function to be executed during the test. In `PGNGUITextFilesTests` function, make the desirable choice uncomment while others two in comment state.





## EditMode Tests

The EditMode tests are prepared for probably programmers who would modify game logic code, namely scripts contained in Mechanism namespace. Most of the time people who perform tests in this section know what they are doing.

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2Fh0Fzb1x9uq7Yu4IUB8Fn%2FUnity_JNvKLZDu6A.png?alt=media&#x26;token=30769edd-15e7-451b-95bf-3de852c6787d" alt=""><figcaption></figcaption></figure>



