Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

358
Visualizações
How can I add vanilla C# code and tests to a Unity project?

I have a Unity (version 2020.2.1f1) project and I've factored out a small utility in C# which I'd like to unit test. Currently in order to compile, the utility class needs to extend MonoBehaviour, but I'd like to avoid this and keep the class pure C#.

I've looked for documentation on unit testing in Unity, but can only find the Unity Test Framework which seems to be geared to game code rather than pure C#. Is there a way to unit test vanilla C# code in Unity?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The following article mentions nothing about a testcase having to include Unity-specific code https://www.raywenderlich.com/9454-introduction-to-unity-unit-testing

Unity IS C# after all. Not everything in your project has to use the Unity API

over 4 years ago · Santiago Trujillo Relatório

0

Why does it need to implement MonoBehaviour?

I would suggest you have e.g.

public class YourClass 
{
    ... 
}
 

and where/if at all needed you wrap it in a

public class YourClassBehaviour : MonoBehaviour
{ 
    public YourClass yourClassInstance; 
}

And then only test the internal functionality of the YourClass.


Either way: It doesn't need to be anything Unity specific in order to unit test it.

You can just go ahead and e.g. do

namespace YourThingy.EditorTests
{
    public class YourClassTest
    {
        private YourClass yourClassInstance;

        [OneTimeSetup]
        public void Setup()
        {
            yourClassInstance = new YourClass();
        }

        [Test]
        public void SomeTest()
        {
            yourClassInstance.DoSomething();
            Assert.AreEuqual("someValue", yourClassInstance.someValue);
        }

        [Test]
        public void SoemTestThatIsntEvenUsingYourClass()
        {
            Assert.IsTrue(1f == (2f * 5f / 10f));
        } 
    }
}

As you can see the Unit test are pure c# and don't necessarily use anything from the Unity API (except of course the test framework itself)

In order to not have to enter play mode just make sure you are using Editor Tests.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda