Unity Test Tools Test, unitytest
Sun Guangdong 2015.9.13
Model based testing framework
StrangeIoc
Https://www.assetstore.unity3d.com/en! /Content/13802
Https://bitbucket.org/Unity-Technologies/unitytesttools
Https://bitbucket.org/Unity-Technologies/unitytesttools/wiki/Home
Http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/test-tools
Http://nsubstitute.github.io/help.html
Nunit and NSubstitute Libraries
Function units included in each software system. In an object-oriented language, the minimum unit of functionality is a method. These methods usually depend on other methods and classes. If you want to test the method, there will be some challenges.
• The first challenge is that external dependencies are not easy to set. For example, some objects may need complex initialization.
• The second challenge is that test verification requires a specific execution path used in other classes of certain behavior.
• Finally, calling external class methods may cause some changes that may not be rolled back in an environment, for example, deleting a real record from the database.
Unit testing is to test unit functions in isolated environments. Isolated from all dependencies. This means that only one execution path can work in a specific test environment.
There are five Test dual modes: simulate virtual objects Dummy object, Test stub, Test spy, Mock and Fake.
Unit Tests Unit test
"Unit test ". In the context of this article, it will be defined as a test:
• Write in code.
• Focus on a single "thing" (method/class ).
• There is no "external dependencies (external dependencies)" (for example, not dependent on the Unity editor or need to connect to the online service database ).
Writing Unit Tests write Unit test
To create a unit test, this package uses NUnit-a very popular framework to help create and execute unit tests.
In addition, it includes NSubstitute-a simulated execution framework (that is, if there is no network, you can simulate network execution), and you can create "fake" objects. These fakes are the objects passed to the test instead of the "real" Object method, the "real" Object in it cannot be tested because it depends on external resources (files, databases, remote servers, etc.) when it is created ).
The following example shows a simple script to manage the player lifecycle:
// A simple component that keeps track of health for game objects.public class HealthComponent : MonoBehaviour{ public float healthAmount; public void TakeDamage(float damageAmount) { healthAmount -= damageAmount; }}
Example of a unit test: (put in the Editor folder)
Using Nunit. framework; [TestFixture] public class HealthComponentTests {[Test] public void TakeDamage_PositiveAmount_HealthUpdated () {// Create a health component with initial health = 50. healthComponent health = new HealthComponent (); health. healthAmount = 50f; health. takeDamage (10f); // method to be tested // assert (verify) that healthAmount was updated. assert. areEqual (40f, health. healthAmount) // (expected, current value )}}
In this unit test example, we can see that
1. A class includes the test [TestFixture] feature.
2. unit Test methods should have the [Test] feature.
3. Test the constructed class. It is the Assert class to be tested, used to interact with it (call the TakeDamage method) and Assert (verify) with the expected result of NUnit.
Unit Test Program
After adding a unit test, we can run the program using the unit test.
Open the included unit test programs from the toolbar menu:
It allows the basic operation of a single test, all tests in the project or all tests that failed before. There are other more advanced options, such as setting to automatically run code compilation. The test programs window displays all test projects that can be displayed from their execution log messages or exception classes by organizing their definitions.
Run the code to run all tests from the command line.
Unity.exe-projectPath PATH_TO_YOUR_PROJECT-batchmode-quit-executeMethod UnityTest. Batch. RunUnitTests-resultFilePath = C: \ temp \ results. xml
* The ResultFilePath parameter is optional. It is used to specify the path for storing the reports generated by running all tests.
Integration Test
Sometimes unit tests are too low-level. It usually needs to test the interaction between multiple components, objects and them. This package contains an integrated testing framework that allows you to create and execute tests to use real game objects and components in a separate "test" scenario.
Write integration test
Different from unit testing, integration testing is not written in code. On the contrary, a new scenario should be added to the project. This scenario will contain test objects, where each definition has a single integration test.
Step by Step
Create a new scenario for testing (it can be a naming convention for these scenarios, so it is easy to delete them and it will be helpful when building the game later ).
Open the run integration test (from the toolbar menu ).
Assertion component asserted component
The assertion component is used to install the invariant GameObjects. You do not need to write any code to set this component-it is all in the editor user interface. It is easy to scale, customizable, and can be configured for your own needs.
Unit Test Program
In the editor, NUnit Framework integration allows you to perform unit tests from within Unity. This means that you can instantiate game objects and operate on them from outside the Unity. We provide a comprehensive test run program test and report results.
NSubstitute library
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.