|
??GameRes??????? ?/??????????
????????Pro & Con
??????????????????TDD?????????????????????????????????????????????????? ??????????????????????????????Unity3D???????????????????????????
??1?????????????I/O??????????UI?????????????????????????
??2??????Unity3D?????????????????????????Unity3D???????????????
?????????????????
??1?TDD???????????????????????????????????????????????????????????????????????????????????TDD??????????????????????????????
??2??????????????????????????????????????????????????????????????
??3?????????????????????????????????????????????????????????????????????????????????????????bug????????????????
????Unity3D????????
??????1????I/O????UI??????????????????????????????????????????????
??????2?Unity5.3.x???editor?????????????????NUnit???NUnit?????????,?????.NET???.??????JUnit(Java),CPPUnit(C++),????xUnit???.??,???JUnit??.U3d??????2.6.4??
???Unity Editor?????????IDE?????????????Unity?API???Unity?????????????????IDE?????????GameObject?
??????Unity5.3.x????????????Unity????????????Unity Test Tool???NSubstitute??????????????
??1.????
??2.????
??3.????
????????Unity Test Tool??NSubstitute????
??????????
??????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??????????????????????Unity??????????
?????????????
??????????????????Unity Editor???????????????????NUnit????
????NUnit??????
??1.?????????
??2.????????????????
??3.??????????
??????????Unity3D????????????NUnit.Framework??????????????[TestFixture]????????????[Test]??????????????Editor?????
??????????
- using UnityEngine;
- using System.Collections;
- using NUnit.Framework;
- [TestFixture]
- public class HpCompTests
- {
- //???????????????????
- [Test]
- public void TakeDamage_BeAttacked_HpEqual()
- {
- //Arrange
- HpComp health = new HpComp();
- health.currentHp = 100;
- //Act
- health.TakeDamage(50);
- //Assert
- Assert.AreEqual(50f, health.currentHp);
- }
- }
复制代码
????????????????????????????
?????????????????????TakeDamage_BeAttacked_HpEqual????????????Nunit?Assert???????????
???????????
??????????????????????????????????????????
????NUnit????????????
??NUnit??C#??????????????????????????????????????????????
???????NUnit??????????????????NUnit.Framework?????
???NUnit?????????????????????
??1.[TestFixture]????????NUnit?????
??2.[Test]???????????????????????????????
????????????????????????????????????[Category]??????????[Ignore]?????????
?????NUnit??????
- [SetUp]
- [TearDown]
- [TestFixture]
- [Test]
- [TestCase]
- [Category]
- [Ignore]
复制代码
???????????
?????????
?????????????????????[ClassName]Tests???
??????????
????????????????????????????????????????[???????]_[?????????]_[????????]?
???????
??1????????
??2????????????????????????????
??3?????????????????????????????????????
???????????????????????
??1?????????????????
??2???????????????
??3???????????
??????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????? ????
?????????3A??
????NUnit????????????????????????????????????????????????
??????????????????????3A????
??1.Arrange???????????????????
??2.Act??????
??3.Assert????????????
??????????????????????NUnit??????????3A????????????????NUnit?????Assert????????HpComp????????TakeDamage?
- using NUnit.Framework;
- [TestFixture]
- public class HpCompTests
- {
- //???????????????????
- [Test]
- public void TakeDamage_BeAttacked_HpEqual()
- {
- //Arrange
- HpComp health = new HpComp();
- health.currentHp = 100;
- //Act
- health.TakeDamage(50);
- //Assert
- Assert.AreEqual(50f, health.currentHp);
- }
- }
复制代码
?????????Assert?
??NUnit???????Assert????????????Asset????????????????????????Assert???????????????????NUnit????????????
??Assert?????????????????
?????
- Assert.AreEqual(???????)?
- Assert.AreEqual(1?2 - 1)?
复制代码
????Assert???????????????????
????????????
???????????????????????
??????????????????????????????????????????????????????????????????
????????????
?????????????????????????????????????????????????????????????????????????????????????
????????????
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??1.switch?if
??2.foreach?for?while
????????????????????????????????????
??????????
???????????????????????????????????????????????????????????????????????????????
?????????????
????????
??????????????????????????????????????????????????
???????????????????????????????????????[TestCase]???????????????????
????????
????????????????????????????????????????????????????
????????????????????????????????????????????
??1?????????????????????????????????????????????????????NUnit?????????????????????????????????????
??2?????????????????
??3????????????????????????????????????????????
????????????
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
????????
???????????????????????????
????????????
????????????????????????????????????????????
???????????
- [Test]
- public void TakeDamage_BeAttacked_HpEqual()
- {
- //Arrange
- HpComp health = new HpComp();
- health.currentHp = 100;
- //Act
- health.TakeDamage(50);
- //Assert
- Assert.AreEqual(50f, health.currentHp);
- }
复制代码
?????????????????50??????????????????????????????????????????????????????????????????????????????????????
- [Test]
- public void TakeDamage_BeAttacked_HpEqual()
- {
- HpComp health = new HpComp();
- health.currentHp = 100;
- health.TakeDamage(50);
- float leftHp = 50f;
- Assert.AreEqual(leftHp, health.currentHp);
- }
复制代码
?????Untiy?????????
???Unity????????????????????Unity??????????????????????NUnit????
?????????????????NUnit.Framework??????????????[TestFixture]????????????[Test]??????????????Editor?????
??????????????3A????Arrange, Act, Assert?
???????????????????????????????
???????????
??????????????
??????????
- using UnityEngine;
- using System.Collections;
- using NUnit.Framework;
- [TestFixture]
- public class HealthComponentTests
- {
- //?????????????0?
- [Test]
- public void TakeDamage_BeAttacked_BiggerZero()
- {
- //Arrange
- UnMonoHealthClass health = new UnMonoHealthClass();
- health.healthAmount = 50f;
- //Act
- health.TakeDamage(60f);
- //Assert
- Assert.GreaterOrEqual(health.healthAmount, 0);
- }
- }
复制代码
?????????????????????????????
?????????????????????TakeDamage_BeAttacked_BiggerZero????????????Nunit?Assert???????????
????Editor Tests Runner???????
????????????????????Unity5.3.x?editor??????????????
???????????????????????????????????????????????????
???????????????????????
??Run All???????
??Run Selected????????
??Rerun Failed? ???????????????
????????????
????????????????????????????????CategoryAttribute????
????????????????????????????
?????????????????????????
??????????????
???????Editor?????????????????????????????????????????U3D???????????U3D????????????????????????????????????????
??Unity3D 5.3.x??????????????
- runEditorTests ?????editor test???
- editorTestsResultFile ????????
- editorTestsFilter ???????????????
- editorTestsCategories ???????????????
- editorTestsVerboseLog ?????????
- projectPath ????
- ?????????????????
- Unity -runEditorTests -projectPath /Users/fanyou/UnitTest -editorTestsResultFile /Users/fanyou/UnitTest/test.xml -batchmode -quit
复制代码
??????
?????????U3D????????????????????????TDD?????????????????????????????????????????????????????????????????????
?????TDD?Unity3D??????????
|
|