Unit Tests in Unity

Поделиться
HTML-код
  • Опубликовано: 27 сен 2024
  • Sign up for the Level 2 Game Dev Newsletter: eepurl.com/gGb8eP
    In this video I'll show you how to write more effective Unit Tests in Unity. More importantly, I'll demonstrate how valuable Unit Tests can be for Unity projects of any size and Unity developers of any skill level.
    #Unity3D #UnityTutorial #GameDevelopment
    📦 Download the code at / 37986282
    👨💻 Join our community: / discord
    ❤️ Support the channel: / infalliblecode
    My Favorite Unity Assets 💯⤵️
    1️⃣ Odin Inspector: assetstore.uni...
    2️⃣ Shapes: assetstore.uni...
    3️⃣ Easy Save: assetstore.uni...
    4️⃣ Dialogue System for Unity: assetstore.uni...
    5️⃣ Editor Console Pro: assetstore.uni...
    ⚡ Learn more about Unity 3D Plus at prf.hn/click/ca...
    👋 Contact me directly at charles@infalliblecode.com
    * Disclosure: These are affiliate links, which means I'll receive a commission if you use them to make a purchase.

Комментарии • 184

  • @InfallibleCode
    @InfallibleCode  4 года назад +12

    NEXT VIDEO: Design Patterns in Game Development
    👉 ruclips.net/video/qS2SPl3GE3k/видео.html

    • @mrkhan8631
      @mrkhan8631 3 года назад

      I have too sheep but it's real

  • @RayBarrera
    @RayBarrera 4 года назад +297

    This format is like watching an E3 gameplay presentation with "real" in-game chat.

    • @Sergalt
      @Sergalt 4 года назад +20

      heh, yea that is not how 2 programmers talk with one another xD

    • @InfallibleCode
      @InfallibleCode  4 года назад +50

      @@Sergalt lol you're right - that's how a crazy programmer talks to himself xD

    • @Sergalt
      @Sergalt 4 года назад +10

      @@InfallibleCode well, the only unrealistic way is how nice you are xD

    • @lesthodson2802
      @lesthodson2802 2 года назад +7

      I hate this format.

  • @47Mortuus
    @47Mortuus 4 года назад +92

    2:43 "And it looks like all the other vectors line up aswell"
    N
    E W
    S
    yap, looks good to me :)

  • @evgenkonyshock4913
    @evgenkonyshock4913 4 года назад +27

    Dialogue feels like an english lesson for beginners where they try to retain pauses between sentences and use most simple words for those who haven't learned complex dictionary

  • @MassimoRough
    @MassimoRough 4 года назад +85

    Hey mate, if you find pressing triangles to browse hierarchy not so excited as the rest of the video may I suggest you pressing it once while holding Alt key to reveal whole structure.

  • @skelhain
    @skelhain 2 года назад +13

    I love how you do not just say "It's good to do this and that!" but actually create a well known scenario that leads to that conclusion. Well done!

  • @ShiNijuuAKL
    @ShiNijuuAKL 4 года назад +28

    Wait a second! they both have the same laugh!

  • @jmd01
    @jmd01 4 года назад +37

    As usual a really nicely produced, clear and interesting video. It would have a made a better demo of testing to have replaced the bug, so that your test was red until you fixed the code.

  • @dordeath4883
    @dordeath4883 2 года назад +3

    This is one of the most comfortable vids for coding ive ever watched, very informative and the creativity of the making of the vid makes this easy to understand

  • @philipvaninting7257
    @philipvaninting7257 4 года назад +27

    "Fair enough"
    -Charles, 2020

  • @DanielAndersen
    @DanielAndersen 4 года назад +10

    Great tutorial, I'm eager to set up robust unit testing in my project. One note: it's not strictly necessary to create a new assembly definition in order to use EditMode tests. As shown in the message at 5:53, EditMode tests can be placed into the "editor special folder". Any script that is placed within a directory named "Editor" will be associated with the Assembly-CSharp-Editor.dll assembly, rather than the Assembly-CSharp.dll assembly. I mention this because my project uses a third-party UI library that is not currently structured in a way to easily create a new assembly definition without massive code reorganization or cyclic dependencies. Using the "Editor" folder won't offer the compile-time improvements of splitting up code into different assemblies, but it will get you up and running with unit tests. Thanks again!

    • @TechnologicNick
      @TechnologicNick Год назад +1

      Thanks a lot, I spent 6 hours messing around with Unity assemblies and your comment just solved my problem!

  • @Spherous
    @Spherous 4 года назад +27

    I think its worth mentioning the 3 A's of unit tests: arrange, act, assert.

    • @InfallibleCode
      @InfallibleCode  4 года назад +14

      That's a great point! I'm thinking of doing follow up to cover some of the basics of writing Unit Tests and the gotchas that are associated with play mode tests.

    • @Spherous
      @Spherous 4 года назад +2

      @@InfallibleCode great idea! I don't know much about them in traditional Unity, I've only written unit tests for DOTS which doesn't suffer from the runtime gotchas.

    • @Zeuts85
      @Zeuts85 2 года назад +5

      Personally I prefer Given, When, Then - because to me it flows more like a coherent thought. "Given these preconditions, when I do X, then Y should happen."

  • @JohnTrustworthy
    @JohnTrustworthy 3 года назад

    I love how you and your alter rubber ducky ego are taking this so calmly and in an upbeat manner.
    I'd like to say that I speak for everyone when I say that debugging usually goes with a lot of silence and a dejected sigh at the end when the bug is finally found. Or loud expletives all the way through. It's funny how there's rarely any in between.

  • @smurfz87
    @smurfz87 3 года назад +3

    Thanks for making these guides! Working as a Java system developer in my day job I cannot put enough emphasis on how important unit tests are once your code base starts growing past the first few thousand lines of code. You implement something new here, and something random breaks over there. With no tests, those kind of bugs are real hard to spot before launch. As many developers-in-training use your guides as a starting point, it would be great if you put some emphasis on code conventions in your examples. Using conventions like given_when_then (givenSheepInScene_whenKeyPressUp_thenSheepMovesUp) or functionName_testState_expectation (move_isUp_sheepMovesUp) will really help in the long run.

  • @MEJHarrison
    @MEJHarrison 3 года назад +1

    Thanks for the video. Like you and your friend Barles, I actually enjoy writing unit tests. I'm new to Unity and decided I was going to start my first project off on the right foot. After a couple weeks of getting up to speed on Unity / VR, I'm ready to start a small project and this video was exactly what I needed to get me going. FYI, love the Charles/Barles thing. That's a fun twist!

  • @mrmogelost6720
    @mrmogelost6720 2 года назад +1

    Man, this tutorial was such great content. I now fully understand what each of the features of the UTF do, and how to implement unit tests into my own fast-growing bug-infested project!

  • @CockroachSlidy
    @CockroachSlidy 4 года назад +2

    Unit tests also useful for debugging. Unity runs them as regular code with all debuggers attached. You can make special tests that reproduce the bug, set breakpoints, and step through code. Saves a lot of time especially for those bugs that occur one in a blue moon.

  • @ExcaliburZero0
    @ExcaliburZero0 2 года назад +2

    This was a really good informative video. The structure of having one person teaching someone else was done quite well and did a good job of getting across the relevant information in an effective and approachable fashion.
    Great job!

  • @luckyknot
    @luckyknot Год назад

    Excellent example about unit testing your videos about learning mid-advanced C#-Unity are the best sir!

  • @AkioJunichiro
    @AkioJunichiro 2 года назад +1

    Thanks you I watch the whole Unity Conference about it and this video was waaaaaay more usefull

    • @InfallibleCode
      @InfallibleCode  2 года назад

      Wow that means a lot! I’m so glad you found it useful :D

  • @ckazo4nikm109
    @ckazo4nikm109 2 года назад

    Not exactly what i was looking for, but this saved my life. Thanks, man.

  • @HassaanALal
    @HassaanALal 2 года назад

    man you are helping TONS of people, thank you.

  • @Edzward
    @Edzward 4 года назад

    Usually I don't like heavily scripted tutorials but I am loving this format! Thanks! Liked, shared and subscribed!

  • @MRHartach
    @MRHartach 2 года назад

    As a start and understand why it is important I think it is a good video ;)

  • @realtimberstalker
    @realtimberstalker 4 года назад +17

    The caller is Charles, so is the other guy... Barles? Is that what he said?

    • @InfallibleCode
      @InfallibleCode  4 года назад +14

      Yep! Thanks for tuning in to The Adventures of Charles and Barles: A Game Developer Slowly Slipping into Insanity!

    • @coolmatt3906
      @coolmatt3906 4 года назад

      @@InfallibleCode Barles is so nice. Is he ever a jerk, like buzz off Charles, I've got real work to do.

  • @CalmHive
    @CalmHive Год назад

    Nice presentation for learners, good teacher!

  • @fernofai9850
    @fernofai9850 3 года назад +12

    Nicely made up, but honestly for me a tutorial shouldn't have such a story-based approach so that I have to wait 3 minutes until the real tutorial starts but still takes a lot more time than necessary because you do this conversation thing.

  • @OllyT294
    @OllyT294 Год назад

    okay I'm ngl, at first I HATED the format this was in, but having stuck through it after the first minute or so it was really REALLY good.
    So, sorry for having so little patience! And if you're reading this, it's definitely worth the lil setup

  • @crash1998100
    @crash1998100 4 года назад +2

    I'd say that was a perfect example when not to waste your time on unit testing

  • @absolutedogy346
    @absolutedogy346 4 года назад +1

    Amazingly well put together video, I appreciate how you have structured it like a tutoring session. It might seem a little cringy at first but I think it really helps with making me engaged like I am actually being taught. Keep up the good work!

  • @AssasinZorro
    @AssasinZorro 2 года назад

    That was a very practical video, thank you!
    As a programmer, I'd say the naming of tests isn't perfect, but that's not the main point here.

  • @MrGolovewkin
    @MrGolovewkin Месяц назад

    Awesome! It might replace the whole unit testing Unity course :)

  • @danielalex1738
    @danielalex1738 Год назад

    Damn this format is so helpfull! Nice job !

  • @javawocky
    @javawocky 3 года назад

    So much cheese and good advice. Thanks - both of you - get the first test failing then passing.

  • @SealDev
    @SealDev 4 года назад +3

    Thank you :) These videos are always quite helpful!

  • @Ziplock9000
    @Ziplock9000 3 года назад

    What I'm finding very useful is for Editor UT's that check to see if my scene has the right root objects, that child objects are set static, that they have shadows turned off.. have collision boxes etc.. Extremely useful as this stuff gets screwed up all of the time and I've been writing my own tech mechanism for this.

  • @goxyazk751
    @goxyazk751 10 месяцев назад

    Awesome tutorial, exactly what I was looking for!

  • @vernongrant4710
    @vernongrant4710 4 года назад

    One of the best tutorials I have ever seen. Thanks!

  • @alokkr029
    @alokkr029 2 года назад

    I like the way of presenting .. basically storytelling...keep going...

  • @MattKander
    @MattKander 4 года назад +2

    Snooped on your Spotify- Griz has some absolute bangers

  • @RobLang
    @RobLang 4 года назад

    Neat introduction. In other cousins areas we tend to define the atomic unit as a domain aggregate as the classes and properties that belong to that aggregate are internal to that feature. In this case, if the move code cannot exist without the player code then all you're doing is reproducing work. The higher up object wins as that is the interaction point.

  • @PaulJonesy
    @PaulJonesy Год назад

    I’ve been a professional developer for decades, I absolutely agree that unit tests pay massive dividends over time, especially as code gets modified over time.

  • @6733199
    @6733199 3 года назад

    Thank you, for a good start to create the unit test in Unity

  • @andrebarreto9652
    @andrebarreto9652 Год назад

    Your videos are always super helpful and easy to follow. Thanks

  • @rubencruz2687
    @rubencruz2687 3 года назад

    Thanks! I guess a complex video to make and in the end, you made it simpler to understand. So, good teaching job, and thank you again!

  • @mykytamarkianov4870
    @mykytamarkianov4870 Год назад

    Those two are really good friends I guess

  • @TaoriUTS
    @TaoriUTS 3 года назад +3

    4:25 to skip to relevant part

  • @nguyenhoangminhtrung2779
    @nguyenhoangminhtrung2779 2 года назад

    love this guy

  • @michailmarkou1995
    @michailmarkou1995 2 года назад

    Great video thank you!

  • @ivanshakhov3759
    @ivanshakhov3759 4 года назад +3

    It is also possible to run/debug tests directly from Rider.

  • @XRCADIA
    @XRCADIA 4 года назад

    Great video, been meaning to dig deeper into unit tests. Thanks.

  • @D3kryption
    @D3kryption 3 года назад

    Thanks buddy. Nice intro to unit testing in Unity :D

  • @Phyche-g5e
    @Phyche-g5e 2 года назад

    Wow... Thanks for the great tutorial!

  • @jocter3594
    @jocter3594 4 года назад +1

    Feels like Charles is messing with your project, he knows exactly when you are in difficulty and pop in ta appear as the good guy, coincidence ? I think not. He's your evil twin.
    Ps: Love your video, so simple to digest, you show us a problem and slowly explain the logic to solve it.

  • @HadiLePanda
    @HadiLePanda 4 года назад

    Nice format as usual, really enjoy it :)

  • @domizianoatticciati5718
    @domizianoatticciati5718 4 года назад +4

    But what if I need to test some kind of manager? Do I need to fill all the dependencies? Sounds like a huge set up there

  • @hermionegreen333
    @hermionegreen333 3 года назад +6

    Very useful but overproduced and a lot of time is wasted on making the video "cute" which is not needed given the topic. I would suggest saving yourself and the viewer time and cutting out the needless talk. You're not talking to third graders.

  • @nhantran8254
    @nhantran8254 5 месяцев назад

    Thank you!

  • @chuchu-je4lj
    @chuchu-je4lj 2 месяца назад

    Very Good!Thanks for help!

  • @oafkad
    @oafkad 3 года назад

    I may not have noticed this was the same guy the first time I watched this.
    (I definitely didn't notice.)

  • @JamesClemones
    @JamesClemones 4 года назад +1

    This is brilliant, thank you

  • @TaeruAlethea
    @TaeruAlethea 3 года назад

    Combine this with the stuff about Interfaces and it all makes so much more sense!!! You can try out new implementation or more advanced logic and then test the other simple stuff works still! It all just clicked into place

  • @jopaslona1
    @jopaslona1 2 года назад

    Great format, great job

  • @kanishktantia2388
    @kanishktantia2388 3 года назад

    Good format for teaching.

  • @cwdgamedev822
    @cwdgamedev822 Месяц назад

    This is a great and high quality tutorial for showing how to set up tests, but I still am not understanding the purpose of the extra work. I don't see what the point of doing anything after 3:18 is. Maybe it would have been helpful to show a test failing and how that would be helpful in resolving bugs? I'd humbly thank anyone willing to help explain the value of tests...

  • @jameslohr6021
    @jameslohr6021 Год назад +1

    Didn't work.
    After creating the "Scripts" assembly, my project stopped compiling. It seems to have broken all dependencies (such as anything related to the InputSystem).
    Instead I used DanielAndersen's method below: instead of messing around with assemblies, I simply created a Tests / Editor folder (note that the name "Editor" is a special folder name, which keeps the code out of your play mode assembly).

  • @hellhunter9478
    @hellhunter9478 2 года назад

    This video is very useful thank you for sharing this with us :)

  • @oz14
    @oz14 2 года назад

    Thanks !

  • @PAXvideo
    @PAXvideo 2 года назад

    Im mostly interested in that spotify coding playlist...

  • @robsonfrancomaciel
    @robsonfrancomaciel 4 года назад

    Thank you man!!!!! You are awesome

  • @appleFullable
    @appleFullable 3 года назад

    Good job mate, helped a lot!

  • @dennisgonzales9521
    @dennisgonzales9521 3 года назад

    Thanks

  • @MaicoRevolucion
    @MaicoRevolucion 3 года назад

    Awesome tutorial!

  • @philippdegasper6509
    @philippdegasper6509 3 года назад

    Hey, Very well done tutorial! Thank you and greetings :-)

  • @FM_GOBi
    @FM_GOBi 4 года назад +2

    What if I make bugs in my Unit Test code? Can I somehow test my tests? 😁

  • @umutcoskun4247
    @umutcoskun4247 3 года назад

    I like that anime laugh at the end :D

  • @fdiaz6376
    @fdiaz6376 4 года назад

    haha I have to say, I laughed a lot watching this. This is a good way to motivate people to learn, at least I do. I am new on this channel, I like your videos. :D

  • @itsrashid9760
    @itsrashid9760 Год назад

    object reference error in test cases when try to compare the gameobject position

  • @blue_lobster_
    @blue_lobster_ 4 года назад +1

    Thank you! Btw, how do you mock dependencies?

  • @Nurpie
    @Nurpie Год назад

    On Mac, is it not supported to dock a window in the editor? On top of that, I can't switch between editor and test runner. Sometimes it happens when I press command+tab but only once.

  • @rStarStudio
    @rStarStudio 4 года назад

    Hope can make more advance video about unit test like your Zelda TDD course.

  • @fangyechannel
    @fangyechannel 4 года назад

    this would be super good if combie with Zenject

  • @michaelfinne617
    @michaelfinne617 Год назад

    Вообще я до этого видео был больше по девочкам, но этот мужчина выглядит и звучит так как если бы я мог доверить ему все мои секреты, а он бы спокойно выслушал бы их и дал свои советы, а потом мы бы занялись горячей любовью.

  • @Holzf43ller
    @Holzf43ller 3 года назад +1

    I like that style and all,and your tutorials come in pretty handy, because i am starting TDD and keeping it up in unity is actually pretty helpfull.
    But i have a error with the PlayMode Test.
    When i begin from ur start.example i get the error "MissingComponentException": There is no SpriteRenderer.
    So i tried the completed, but i still got a failed test in PlayMode.
    So i added a SpriteRenderer with:
    gameObject.AddComponent();
    before the line
    var sheep = gameObject.AddComponent();
    then it actually ran and said: eppected(0,1,0) but was(0,-1,0)

    • @christofstanits
      @christofstanits 2 года назад

      you still need to fix the original bug in the sheep class. Line 25

  • @SenthilKumar-gz6rz
    @SenthilKumar-gz6rz 4 года назад +4

    Hi man.That was a Great Stuff on Unit Testing. But it was too small, I mean who will unit test on direction with only one spec. I dont get the need for unit test. its too basic. on the whole nice work dude

  • @JJZzZzZzZ
    @JJZzZzZzZ 3 года назад +1

    What if there's bug in the unit test code

    • @InfallibleCode
      @InfallibleCode  3 года назад +2

      Then the tests wouldn’t be correct. That’s why you should make sure that each test method tests a single, discrete thing in the simplest way possible.

  • @gorkemvids4839
    @gorkemvids4839 2 года назад

    Hi, thanks for tutorial but i could not use testing for dotween. I keep having those "does not contain definition" errors for scripts who use dotween

  • @Layarion
    @Layarion 3 года назад

    why does mono need to be in playmode? you didn't explain why.

  • @SlothHuntOnYou
    @SlothHuntOnYou 2 года назад

    5:45 - NICE! I got it, but all other things is does not work for me=)) But I have a NICE folder for today

  • @aadityakiran_s
    @aadityakiran_s 4 года назад

    Only if I had somebody like that to tell me all this stuff in my company. But I still don't see how you can write tests for everything that we need to do. I wish there was some comprehensive course or something on this.

  • @DeathxStrike18
    @DeathxStrike18 2 года назад

    Sheep class should probably be renamed to Character class, what if you want wolves that bounce as well

  • @notBradYourself
    @notBradYourself 3 года назад

    Why would east be x-1? shouldn't it be x + 1?

  • @Tiparium_NMF
    @Tiparium_NMF 3 года назад

    Is it possible to do tests on items created in the Hierarchy window as opposed to the Project dropdown?
    I created several component scripts in the Project window, but none of them are ever individually instantiated as assets. Instead they act as components to objects created within the scene. Is it possible for me to test these or do I need to change up how my code is organized?

  • @Shabazza84
    @Shabazza84 4 месяца назад

    Nice. But no one demonstrates how to test scripts that inherit from MonoBehavior...
    You can't call new() on such a class. So how should I test my MonoBehavior Scripts then ? (which is naturally 90% of them...)
    EDIT: Nevermind. You can just do this in the unit test script's test method:
    var TestObj = new GameObject().AddComponent();

  • @Tiparium_NMF
    @Tiparium_NMF 3 года назад

    Can you dive a little more into what would make something an EditMode vs a PlayMode test? I don't get what makes something one or the other.

    • @StephanTrube
      @StephanTrube Год назад

      PlayMode tests are for things which need a running Unity Scene, for example MonoBehaviours on GameObjects.
      EditMode tests are for your other code, which is not a MonoBehaviour.
      If your logic needs to live on a GameObject to function properly --> PlayMode
      Else --> EditMode
      At least that's my current understanding, just learning.

  • @royorgames771
    @royorgames771 4 года назад

    Hi Charles/Barles =)
    Thank you for the videos. I like to use the more serious parts that you show.
    It really does simplify the code and makes it easier to reuse!
    I have some UnityTest, which is almost the same, except the parameters.
    For my normal tests I can use the [TestCase(5f,true)] attribute for e.g. TakeDamageTest(float damage, bool isAliveAfterwards) and easily test multiple scenarios with the same test.
    Can this be done for the [UnityTest] as well somehow?

  • @jakovd
    @jakovd 4 года назад

    Hey, I'd like to check your full Coding Music playlist. I've noticed you have a good taste in music :)

  • @brendankhaw6695
    @brendankhaw6695 4 года назад

    Hey how would you test sound for unit testing? Sorry new to this

  • @小鳥ちゃん
    @小鳥ちゃん 4 года назад +1

    But is it worth writing unit tests for a small projects with only one person working on it? I can't see how to use unit testing in my projects.

    • @Max-up8vj
      @Max-up8vj 4 года назад

      youre writing those tests for yourself, regardless of project size. all projects start small, but as your project grows you will get to a point where you wish you wrote tests earlier, because its hard to write them for large projects.

    • @小鳥ちゃん
      @小鳥ちゃん 4 года назад +1

      @@Max-up8vj I mean, I'm sure that project won't be any bigger and I may spend more time on writing tests then get benefits from that, idk

    • @Max-up8vj
      @Max-up8vj 4 года назад

      @@小鳥ちゃん ofc you shouldnt write them if there are no benefits

  • @Luis-Torres
    @Luis-Torres 4 года назад +2

    Am I the only one who thinks east and west are flipped in the video? ruclips.net/video/PDYB32qAsLU/видео.html (Idk how to make this just the time)
    east's x value should be 1 while west's value should be -1.

    • @ishtilkind
      @ishtilkind 4 года назад

      EAST is left (-1) and WEST is right (1).