Skip to content

Instantly share code, notes, and snippets.

@terence410
Last active May 15, 2019 11:05
Show Gist options
  • Save terence410/2d13b04e99fbecabc12aa93cfc40f2bc to your computer and use it in GitHub Desktop.
Save terence410/2d13b04e99fbecabc12aa93cfc40f2bc to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.TestTools;
namespace Tests
{
[Serializable]
public class SearchResult
{
public string name;
public string latest;
}
public struct SearchJson
{
public SearchResult[] results;
public int total;
}
public class TestConnection
{
[SetUp]
public void SetUp() {
//SetUp runs before all test cases
Debug.Log($"--- Setup --- ");
}
[TearDown]
public void TearDown() {
//SetUp runs after all test cases
Debug.Log($"--- TearDown --- ");
}
[UnityTest]
public IEnumerator Test_JSON()
{
string url = "https://api.cdnjs.com/libraries?search=mocha";
UnityWebRequest www = UnityWebRequest.Get(url);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
Assert.IsTrue(false);
}
else
{
yield return TestHelper.Timeout(1.0);
// Show results as text
var json = JsonUtility.FromJson<SearchJson>(www.downloadHandler.text);
Debug.Log($"{www.downloadHandler.text}");
Assert.GreaterOrEqual(json.total, 1);
Assert.GreaterOrEqual(json.results.Length, 1);
Assert.AreEqual(json.results[0].name, "mocha");
}
// Use the Assert class to test conditions.
// yield to skip a frame
yield return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment