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.
NEXT VIDEO: Design Patterns in Game Development
👉 ruclips.net/video/qS2SPl3GE3k/видео.html
I have too sheep but it's real
This format is like watching an E3 gameplay presentation with "real" in-game chat.
heh, yea that is not how 2 programmers talk with one another xD
@@Sergalt lol you're right - that's how a crazy programmer talks to himself xD
@@InfallibleCode well, the only unrealistic way is how nice you are xD
I hate this format.
2:43 "And it looks like all the other vectors line up aswell"
N
E W
S
yap, looks good to me :)
xddd
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
lmao
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.
Dayum, didn't know that O.o Thanks a lot!
BEST PART OF THIS TUTORIAL! Thanks Maks!
lifesaver, thank you!
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!
Wait a second! they both have the same laugh!
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.
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
"Fair enough"
-Charles, 2020
😂
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!
Thanks a lot, I spent 6 hours messing around with Unity assemblies and your comment just solved my problem!
I think its worth mentioning the 3 A's of unit tests: arrange, act, assert.
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.
@@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.
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."
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.
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.
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!
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!
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.
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!
Excellent example about unit testing your videos about learning mid-advanced C#-Unity are the best sir!
Thanks you I watch the whole Unity Conference about it and this video was waaaaaay more usefull
Wow that means a lot! I’m so glad you found it useful :D
Not exactly what i was looking for, but this saved my life. Thanks, man.
man you are helping TONS of people, thank you.
Usually I don't like heavily scripted tutorials but I am loving this format! Thanks! Liked, shared and subscribed!
As a start and understand why it is important I think it is a good video ;)
The caller is Charles, so is the other guy... Barles? Is that what he said?
Yep! Thanks for tuning in to The Adventures of Charles and Barles: A Game Developer Slowly Slipping into Insanity!
@@InfallibleCode Barles is so nice. Is he ever a jerk, like buzz off Charles, I've got real work to do.
Nice presentation for learners, good teacher!
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.
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
I'd say that was a perfect example when not to waste your time on unit testing
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!
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.
Awesome! It might replace the whole unit testing Unity course :)
Damn this format is so helpfull! Nice job !
So much cheese and good advice. Thanks - both of you - get the first test failing then passing.
Thank you :) These videos are always quite helpful!
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.
Awesome tutorial, exactly what I was looking for!
One of the best tutorials I have ever seen. Thanks!
I like the way of presenting .. basically storytelling...keep going...
Snooped on your Spotify- Griz has some absolute bangers
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.
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.
Thank you, for a good start to create the unit test in Unity
Your videos are always super helpful and easy to follow. Thanks
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!
Those two are really good friends I guess
4:25 to skip to relevant part
love this guy
Great video thank you!
It is also possible to run/debug tests directly from Rider.
Great video, been meaning to dig deeper into unit tests. Thanks.
Thanks buddy. Nice intro to unit testing in Unity :D
Wow... Thanks for the great tutorial!
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.
Nice format as usual, really enjoy it :)
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
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.
Thank you!
Very Good!Thanks for help!
I may not have noticed this was the same guy the first time I watched this.
(I definitely didn't notice.)
This is brilliant, thank you
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
Great format, great job
Good format for teaching.
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...
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).
This video is very useful thank you for sharing this with us :)
Thanks !
Im mostly interested in that spotify coding playlist...
Thank you man!!!!! You are awesome
Good job mate, helped a lot!
Thanks
Awesome tutorial!
Hey, Very well done tutorial! Thank you and greetings :-)
What if I make bugs in my Unit Test code? Can I somehow test my tests? 😁
I like that anime laugh at the end :D
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
object reference error in test cases when try to compare the gameobject position
Thank you! Btw, how do you mock dependencies?
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.
Hope can make more advance video about unit test like your Zelda TDD course.
this would be super good if combie with Zenject
Вообще я до этого видео был больше по девочкам, но этот мужчина выглядит и звучит так как если бы я мог доверить ему все мои секреты, а он бы спокойно выслушал бы их и дал свои советы, а потом мы бы занялись горячей любовью.
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)
you still need to fix the original bug in the sheep class. Line 25
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
What if there's bug in the unit test code
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.
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
why does mono need to be in playmode? you didn't explain why.
5:45 - NICE! I got it, but all other things is does not work for me=)) But I have a NICE folder for today
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.
Sheep class should probably be renamed to Character class, what if you want wolves that bounce as well
Why would east be x-1? shouldn't it be x + 1?
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?
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();
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.
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.
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?
Hey, I'd like to check your full Coding Music playlist. I've noticed you have a good taste in music :)
Hey how would you test sound for unit testing? Sorry new to this
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.
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.
@@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
@@小鳥ちゃん ofc you shouldnt write them if there are no benefits
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.
EAST is left (-1) and WEST is right (1).