Unity Unit Testing with Socket.io

Explore the beauty of unit test

Terence Tsang
3 min readMay 16, 2019

Preparation

Key Remarks

  • In testing tear down, you must disconnect your socket client. After the test ended, the socket may still exist and running in background
  • You may update MaxConnectionPerServer = 4; in HTTPManager if you found that there are some weird case makes your failed to connect sometime
  • Please user different UserId to avoid having connect fight with other, you can use any numeric string for UserId

Objective A

For client team only.

Implement Unit Test, covering all test cases in NodeJs, in Unity3D in Editor Mode. All test cases need to be able to run all together.

Objective B

For server team only.

Implement the Socket.io Server and able to pass all the Unit Tests.

The server side codes has around 300 line of codes all together.

You can either use TypeScript /Javascript / PHP to implement it.

Server Details

Unity Quick Start

private SocketOptions _options = new SocketOptions();
_options.AdditionalQueryParams = new ObservableDictionary<string, string>();
_options.AdditionalQueryParams.Add("userId", userId);
_options.ConnectWith = TransportTypes.WebSocket;
Uri _uri = new Uri("https://blooming-fjord-24277.herokuapp.com/socket.io/");
SocketManager _manager = new SocketManager(_uri, _options);
_manager.Encoder = new LitJsonEncoder();
_socket = _manager.Socket;
_socket.On("connect", OnConnect);
_socket.On("disconnect", OnDisconnect);
_socket.On("error", OnError);
_socket.On("event", OnEvent);
_socket.Emit("getRoom"); // server: respondGetRoom
_socket.Emit("leaveRoom"); // server: respondLeaveRoom
_socket.Emit("createRoom"); // server: respondCreateRoom
_socket.Emit("joinRoom", "roomId"); // server: respondJoinRoom
_socket.Emit("chatInRoom", "message"); // server: respondChatInRoom
respondMessageinRoom // event from server, when someone send message to room
broadcast // a event emitted from server every 1 second
// return format from server
{isSuccess: boolean, roomId: string, message: string}

Unity Client Side Test Case Setup

  • Create Unity Project
  • Unzip Best HTTP (Pro).zip
  • Create Assets/ThirdParties, create an assembly definition (name it to ThirdParties) and drag the “Best Http (Pro)” folder into it
  • Create Assets/Tests, create an assembly definition (name it as Tests). Check the box “Test Assemblies” and select “ThirdParties” in the Assembly Definition References. You must only select “Editor” in Platform.
  • Create a test script: Create > Testing > C# Test Script
  • Open Window > General > Test Runner
The settings will look like above

Client Side Test Scripts

NodeJs Client Side Test Case Setup

// install NodeJs 10 https://nodejs.org/en/
mkdir socketio-unit-test
mkdir socketio-unit-tes/tests
mkdir socketio-unit-tes/tests/Helpers
cd socketio-unit-test
// copy below files into corresponding folders
npm install
npm run "test prod"

./package.json

{
"name": "heroku-testing",
"version": "1.0.0",
"description": "",
"main": "build/App.js",
"dependencies": {
"socket.io": "^2.2.0",
"socket.io-client": "^2.2.0",
"debug": "^4.1.1"
},
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.6",
"@types/node": "^11.13.5",
"@types/socket.io": "^2.1.2",
"@types/socket.io-client": "^1.4.32",
"@types/debug": "^4.1.4",
"chai": "^4.2.0",
"mocha": "^6.1.4",
"ts-node": "^8.1.0",
"typescript": "^3.4.5"
},
"scripts": {
"test local": "set DEBUG=app* & set HOST=http://127.0.0.1:80 & mocha --exit --timeout 10000 -r ts-node/register tests/*.ts",
"test prod": "set DEBUG=app* & set HOST=https://blooming-fjord-24277.herokuapp.com & mocha --exit --timeout 10000 -r ts-node/register tests/*.ts"
}
}

./tsconfig.json

{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"outDir": "./build",
"rootDir": "./",
"strict": true,
"esModuleInterop": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"tests/**/*"
]
}

./src/SocketClient.ts

./tests/TestConnection.ts

./tests/Helpers/TestHelper.ts

--

--