游戏开发论坛

 找回密码
 立即注册
搜索
查看: 9230|回复: 1

??NUnit?Unity3D????????????

[复制链接]

1万

主题

1万

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
32147
发表于 2016-7-7 14:15:36 | 显示全部楼层 |阅读模式
640.jpg


??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?????

??????????
  1. using UnityEngine;
  2. using System.Collections;
  3. using NUnit.Framework;

  4. [TestFixture]
  5. public class HpCompTests
  6. {
  7.   //???????????????????
  8.   [Test]
  9.   public void TakeDamage_BeAttacked_HpEqual()
  10. {
  11.   //Arrange
  12.       HpComp health = new HpComp();
  13.       health.currentHp = 100;
  14.      //Act
  15.       health.TakeDamage(50);
  16.      //Assert
  17.       Assert.AreEqual(50f, health.currentHp);
  18.     }
  19. }
复制代码

????????????????????????????

?????????????????????TakeDamage_BeAttacked_HpEqual????????????Nunit?Assert???????????

???????????

??????????????????????????????????????????

????NUnit????????????

??NUnit??C#??????????????????????????????????????????????

???????NUnit??????????????????NUnit.Framework?????

???NUnit?????????????????????

??1.[TestFixture]????????NUnit?????

??2.[Test]???????????????????????????????

????????????????????????????????????[Category]??????????[Ignore]?????????

?????NUnit??????
  1. [SetUp]
  2. [TearDown]  
  3. [TestFixture]
  4. [Test]
  5. [TestCase]
  6. [Category]
  7. [Ignore]
复制代码

???????????

?????????

?????????????????????[ClassName]Tests???

??????????

????????????????????????????????????????[???????]_[?????????]_[????????]?

???????

??1????????

??2????????????????????????????

??3?????????????????????????????????????

???????????????????????

??1?????????????????

??2???????????????

??3???????????

??????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????? ????

?????????3A??

????NUnit????????????????????????????????????????????????

??????????????????????3A????

??1.Arrange???????????????????

??2.Act??????

??3.Assert????????????

??????????????????????NUnit??????????3A????????????????NUnit?????Assert????????HpComp????????TakeDamage?
  1. using NUnit.Framework;

  2. [TestFixture]
  3. public class HpCompTests
  4. {
  5.   //???????????????????
  6.   [Test]
  7.   public void TakeDamage_BeAttacked_HpEqual()
  8. {
  9.   //Arrange
  10.       HpComp health = new HpComp();
  11.       health.currentHp = 100;
  12.      //Act
  13.       health.TakeDamage(50);
  14.      //Assert
  15.       Assert.AreEqual(50f, health.currentHp);
  16.     }
  17. }
复制代码

?????????Assert?

??NUnit???????Assert????????????Asset????????????????????????Assert???????????????????NUnit????????????

??Assert?????????????????

?????
  1. Assert.AreEqual(???????)?
  2. Assert.AreEqual(1?2 - 1)?
复制代码

????Assert???????????????????

????????????

???????????????????????

??????????????????????????????????????????????????????????????????

????????????

?????????????????????????????????????????????????????????????????????????????????????

????????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??1.switch?if

??2.foreach?for?while

????????????????????????????????????

??????????

???????????????????????????????????????????????????????????????????????????????

?????????????

????????

??????????????????????????????????????????????????

???????????????????????????????????????[TestCase]???????????????????

????????

????????????????????????????????????????????????????

????????????????????????????????????????????

??1?????????????????????????????????????????????????????NUnit?????????????????????????????????????

??2?????????????????

??3????????????????????????????????????????????

????????????

??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

????????

???????????????????????????

????????????

????????????????????????????????????????????

???????????
  1. [Test]
  2. public void TakeDamage_BeAttacked_HpEqual()
  3. {
  4. //Arrange
  5.     HpComp health = new HpComp();
  6.     health.currentHp = 100;
  7.    //Act
  8.     health.TakeDamage(50);
  9.    //Assert
  10.     Assert.AreEqual(50f, health.currentHp);
  11. }
复制代码

?????????????????50??????????????????????????????????????????????????????????????????????????????????????
  1. [Test]
  2. public void TakeDamage_BeAttacked_HpEqual()
  3. {
  4.     HpComp health = new HpComp();
  5.     health.currentHp = 100;

  6.     health.TakeDamage(50);

  7.     float leftHp = 50f;

  8.     Assert.AreEqual(leftHp, health.currentHp);
  9. }
复制代码

?????Untiy?????????

???Unity????????????????????Unity??????????????????????NUnit????

?????????????????NUnit.Framework??????????????[TestFixture]????????????[Test]??????????????Editor?????

??????????????3A????Arrange, Act, Assert?

???????????????????????????????

???????????

??????????????

??????????
  1. using UnityEngine;
  2. using System.Collections;
  3. using NUnit.Framework;

  4. [TestFixture]
  5. public class HealthComponentTests
  6. {
  7.   //?????????????0?
  8.   [Test]
  9.   public void TakeDamage_BeAttacked_BiggerZero()
  10.     {
  11.       //Arrange
  12.       UnMonoHealthClass health = new UnMonoHealthClass();
  13.       health.healthAmount = 50f;

  14.       //Act
  15.       health.TakeDamage(60f);

  16.       //Assert
  17.         Assert.GreaterOrEqual(health.healthAmount, 0);
  18.     }
  19. }
复制代码

?????????????????????????????

?????????????????????TakeDamage_BeAttacked_BiggerZero????????????Nunit?Assert???????????

????Editor Tests Runner???????

????????????????????Unity5.3.x?editor??????????????

641.png

642.png

???????????????????????????????????????????????????

???????????????????????

??Run All???????

??Run Selected????????

??Rerun Failed? ???????????????

????????????

????????????????????????????????CategoryAttribute????

????????????????????????????

643.png

?????????????????????????

??????????????

???????Editor?????????????????????????????????????????U3D???????????U3D????????????????????????????????????????

??Unity3D 5.3.x??????????????
  1. runEditorTests  ?????editor test???
  2. editorTestsResultFile ????????
  3. editorTestsFilter ???????????????
  4. editorTestsCategories ???????????????
  5. editorTestsVerboseLog ?????????
  6. projectPath ????
  7. ?????????????????
  8. Unity -runEditorTests -projectPath /Users/fanyou/UnitTest -editorTestsResultFile  /Users/fanyou/UnitTest/test.xml -batchmode -quit
复制代码

??????

?????????U3D????????????????????????TDD?????????????????????????????????????????????????????????????????????

?????TDD?Unity3D??????????

5

主题

266

帖子

809

积分

高级会员

Rank: 4

积分
809
发表于 2016-7-7 18:00:44 | 显示全部楼层
??windows???????mac os????????windows??????????????????????????????????????????????????
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-18 07:08

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表