# How do you create test for unity variables that are set through the [SerializeField] tag?

I need to create some tests for a bunch of variables that are initialized via the editor, So in the script they are always null and can't be set publicly.

Usually using the tag is a good way to encapsulate the code but with tests how do I check such a variable? Example:

public class thing
{
[SerializeField]
private string line;

//constructor(s), methods, etc..

public void setLine(string text){line = text; }
}


How do I check the value of the string after it is set inside a test?
(preferably without creating a getter method)

## 2 Answers

I ended up creating a constructor for each class that i wanted to test. So for the example above it would be:

thing(string line)
{
//this will be the value of a [SerializeField] variable
this.line = line;

//ensure that start is called just like it would in the game
Start();
}


Then in a test I just initialize the class with this value. This works out well as you can integrate all the start method stuff into the initialization which saves you calling it separately within the test.

Your initial code is initialized through the inspector, unless you call your function in void Update() the value will always be null or its initial value no matter what you put in the inspector.

• I think you might have misunderstood. The question is about tests that are made inside unity, These aren't initialized the same way so there is a need to manually set the variables that the script expects. Unless what yous saying is that once I call update all the values are taken from the current scene that the script is in (which I image would only cause confusion)? Feb 18 '16 at 16:53
• I thought you wanted to change values in an active scene with your function, and I said that it would not work unless called in unity's functions such as start, awake, update, etc. So I probably didn't understand correctly. @user3797758 Feb 19 '16 at 8:13
• I'm talking about unit testing, you can google it (there is lots of info out there) but here is an article related to unity: blogs.unity3d.com/2014/07/28/… Feb 20 '16 at 2:01