How to do basic unit testing [Unity/C# tutorial]

  Рет қаралды 20,509

Mina Pêcheux

Mina Pêcheux

Күн бұрын

Пікірлер: 45
@minapecheux
@minapecheux 2 жыл бұрын
As promised at the end of the video, here are some further references and math extras for the curious :) 1. If you’re interested in this idea of “re-defining the basics of maths”, you might want to take a look at the notion of algebraic structures. Those are core math objects that basically start with fuzzy and unconstrained sets of operators and operations, and then as you add more rules and particularities build up into the sets of elements we know and love like the integers, the reals, the complex numbers or even geometrical transformations :) 2. The canonical testing hierarchy, with test suites and test cases, is interesting from the angle of dependency, too. In theory, you should be able to run two test cases in parallel without them conflicting with each other. This means that, quite often, when using unit testing, your logic should be as self-contained as possible - and so you’ll quickly lean towards functional programming (and its pure functions). 3. My example of a basic matrix C# class relies heavily on operator overloading (that you can see in detail in the Github repository 🚀github.com/MinaPecheux/UnityTutorials-UnitTesting), as well as explicitly throwing some exceptions - I might make some additional tutorials on these topics to dive into this a bit more! And that's all for me for now, cheers and happy new year! :D
@ibrahimalhas7442
@ibrahimalhas7442 9 ай бұрын
lovely tutorial! im an unreal engine dev but it's always good to see comparative tutorials that can be applied to any game engine like this:)
@minapecheux
@minapecheux 9 ай бұрын
Glad you enjoyed it, thanks for the nice comment! :)
@iancherabier5920
@iancherabier5920 Жыл бұрын
Really cool! I didn't know about the unity unit test package, I enjoyed your introduction very much! :)
@andrewallbright
@andrewallbright 2 жыл бұрын
Great video! I'm the Andrew :) Very cool! I think this does an excellent job describing the problem space and some of the practical concerns/value propositions of testing. I do think that the subject of automated testing for video game software is a shockingly unexplored (or at least uncommunicated) subject. Knowing how to write good tests is a challenging skill to develop no matter software category. I think in our slice of time we need to look outside the games industry for guidance on automated testing for games.
@andrewallbright
@andrewallbright 2 жыл бұрын
Short list of my own philosophy and practice - I try to encapsulate game logic in vanilla C# classes. That lets me run 'edit mode sync' tests, which are the fastest category. I keep in mind that these classes will be consumed by MonoBehaviours and ScriptableObjects. - I see "GetComponent()" calls as a form of special dependency injection. If your monobehaviour scripts are collaborating with other scripts, consider creating a seam using an abstract interface technique (interface, base class, abstract class, etc). - I consider tests written against a prefab as a form of 'integration test.' This is usually where I write user input tests. It's integration since a prefab is a game object with monobehaviour scripts on it. - I consider tests written against > 1 prefabs and scenes a form of 'acceptance' tests. - GameCI is fantastic and helps you build a unity based CI/CD pipeline in any of the popular version control solutions. I recommend setting it up at the start of a project. Final thought: you are not wrong if you do not write tests for your software. It is a practice that has pros and cons. I am a believer in testing but I also acknowledge that at the end of the day working software is working software. I hope this helps someone!
@andrewallbright
@andrewallbright 2 жыл бұрын
I can't seem to share some other resources, which makes me sad. Oh well! Cheers everyone.
@minapecheux
@minapecheux 2 жыл бұрын
@@andrewallbright Haha, hi Andrew! And thanks for your super nice+interesting comment, it's really great of you to share your own thoughts on the topic with the viewers ;) I especially want to stress out your point about testing not being mandatory: I did say it's essential in the video but that was sort of an exaggeration for the purpose of the tutorial. It is absolutely true that testing can have downsides (first and foremost: it's time-consuming!), so it won't suit all projects... Thanks again, and cheers! :D
@devyoyo7386
@devyoyo7386 2 жыл бұрын
This video is so great as i start to learn the unit testing. I love this!
@minapecheux
@minapecheux 2 жыл бұрын
Thanks a lot for your nice comment - I'm really happy you find the video useful! :)
@luckyknot
@luckyknot Жыл бұрын
Great topic and example @minapecheux, though being too amateurish yet I didn't understood how you link the stub or mockup code that you use on the test classes to verify that the tests work well with the real feature. Should you substitute the stub code by the real code once you code the real implementation? I read on a Xtreme programming book that's not always the case, that's the most difficult part for me to understand, if we do not test the real code, what use the tests have? If would also be very interesting on delving about how 'time-consuming' unit tests are... maybe the save time in the long run for midsized, more complex projects? Also, there is a video called 'TDD, Where Did It All Go Wrong (Ian Cooper)' that tells -after his 20 years of experience- to test the features, no every class or method, otherwise the maintenance cost can defeat the purpose of UT. Would be awesome to know what @andrewallbright658 thinks as he has on-the-field experience with UT I assume.
@minapecheux
@minapecheux Жыл бұрын
Thanks, I'm glad you liked the video :) From my own experience, you don't usually replace the stub data/inputs. Basically, the idea of these mockups is to assume you have the right context, and test whether the feature works properly in that case - but without having to also ensure that you properly build said context! So you decouple the setup from the actual chunk you want to test (by just "forcing the context to be right" with mockup data), and thus you're sure that if it fails, its because the feature you wanted to test doesn't work, and not because of an improper context. I hope it clears things out a bit, feel free to re-ask me if it's still unclear! And yep test-driven dev is definitely a strange mindset that can be super consuming both for the humans writing it, and for the computers running the tests... I think that, as always, it's a question of finding the right trade-off between a bit more computation, and a bit less dev iterations or re-deployments ;)
@luckyknot
@luckyknot Жыл бұрын
@@minapecheux check 'Unit Test in Unity' by Infallible Code, he shows an example with real game code. As I keep modifying my game, I want to be sure that every time that I run my tests, they tell me if the real code is broken or not. I guess the mockups are good to make you think of the in&outs of the real class or objects you will define after or along the unit test, to know what are the basic and edge cases to take into account.... it/s difficult to me to think in mockup data, I would never be sure if my code is bug-free or not. If would be awesome to have a survey that show the % of experienced Unity devs that use real code vs mockup data in unit tests or a 50-50 mix of both? Regarding the 'context' you mention I got you. I'm just a beginner but I guess for testing context stuff you should also have integration tests in place, which btw I haven't seen any info on how carrying about in Unity. Would be interesting that someone with experience showed us how to carry them. There-s a video called 'How to Write Tests for Your Unity Game' by Kodo Linija and this guy may be using them in some of his other videos but it's still too advanced for me :) Thanks for your answer!
@flowed9753
@flowed9753 Жыл бұрын
So useful, thanks a lot for this video!
@minapecheux
@minapecheux Жыл бұрын
You're very welcome! Glad you like it :)
@Shabazza84
@Shabazza84 7 ай бұрын
I don't get how you can reference your actual code in the unit test prijec t. For me, Unity creates a whole new project next to my productive project for unit testing. And no references to that project. So I can't create objects of the clases in my project in the unit test project...?
@minapecheux
@minapecheux 7 ай бұрын
Heya! Hmm, in my own experience, you can setup your unit testing in the same Unity project as your actual app/game, and so everything is in the same "compilation space", and you can reuse your classes... that being said, perhaps it's changed since I made this tutorial!
@maramzaidan4450
@maramzaidan4450 Жыл бұрын
Thank you that was amazing !! can you give us a brief on how we do the same for an already-developed game? how can we test it using unit test framework
@minapecheux
@minapecheux Жыл бұрын
Thanks, I'm glad you liked the tutorial! So if you want to implement tests for an already existing game, there is no "one-size-fits-all" technique... but you basically have to try and extract the different features of your game into testable chunks, and then build the tests gradually. For example, if you're working on a Pong-like game, you could have some unit tests for the initial position of the paddles, and then what happens if you simulate an "up" input, and then what happens to the ball's velocity if it hits the paddle... Sorry I can't really give you a "walkthrough" because it is highly dependent on the game, but that's the basic idea: identify the important scenarios/situations the player will encounter in your game, and replicate them in your tests to check they happen as intended! Hope it helps, cheers ;)
@maramzaidan4450
@maramzaidan4450 Жыл бұрын
@@minapecheux Thank you very much for the clarification!!! , well I am a manual game tester and working on testing voice chat rooms game, created by unity - c# now I have couple of things to deal with, first that I don't have the code but I can ask the help of the developers second,I have the basics of c# but am not a pro so I want to learn more about it , same as well for unity tool But I really want to get in deeper with automation testing so I came across your video and thought it could be a simple method to start with could you please advise me on how can I go deeper with automation testing using the unit test in this case? could it work?
@minapecheux
@minapecheux Жыл бұрын
@@maramzaidan4450 Hi! So I'm not a super expert on the topic of unit testing and test automations, but my first feel about your use case is that applying automation here could be difficult. Let me explain: 1) In order to use unit testing, you have to be able to cut down the feature to test into small chunks that you can have a computer test for you. Would that be possible for you? What sort of things do you usually test manually? Do you test very small features (eg: "does that button work when I click on it?"), or do you test entire processes and logics? If that's the latter, chances are it will be complex and time-consuming to transform them into automated tests. 2) How much knowledge of the game's codebase do you have? Making unit tests isn't easy because you have to know exactly how the features work, so that you can setup a "sandbox context" to reproduce what the game should look like at a given point, and then execute the feature you want to test to check it transforms the state as intended. (We call that "mock data" if you want to browse the net for more info ;) ). This might be because I'm not familiar enough with the topic but, from what I understand, unit testing is usually something that tries to replace manual testing but that is done by the devs of the team (in collab with the game designers and QAs, if need be, to know what to check for)... Again - it might be worth digging more into, because I could miss something, but I hope it helps! Cheers :)
@hassaanmustafa4159
@hassaanmustafa4159 Жыл бұрын
Hi. Can you make a video about which part of the unity game is executed on which part of the device for example CPU, GPU, RAM, Storage etc. And how can we improve each of these. There is not a single video available for this.
@minapecheux
@minapecheux Жыл бұрын
Hi, and first of all - many thanks for the suggestion! That would indeed be a very cool topic! Although I believe it would be quite hard to clearly separate all the parts of a game in terms of hardware component like this :thinking: I mean from what I know there are some clear-cut things, like the shaders executed on the GPU or the C# scripts run on the CPU, but RAM/Storage would be really difficult to tackle. And there are even some features that are run on either one or the other component depending on how you implement them in Unity (eg: particles are computed on the CPU with the legacy Shuriken system, and on the GPU with the newer graph-based VFX tool). So it's a really interesting idea, but I'm not sure how to approach it yet... I'll keep it in a corner of my mind, tho, many thanks for suggesting it :)
@Cal-xq6im
@Cal-xq6im Жыл бұрын
Thanks for making this video, your Scriptable Objects video helped me a lot! Are there simpler examples for Unit Testing, or is this mainly to ensure a more complex function does what it needs to do? I'm thinking this would mostly be used in LeetCode type issues, whereas I can't think of a legitimate use for simple UI or 2D games.
@minapecheux
@minapecheux Жыл бұрын
Thanks, I'm glad the tutorials were useful! :) So, unit testing can be applied to various examples - in my own experience, it can be used to test both very simple/granular functions, and more complex ones; but testing complex functions mostly means calling the unit test methods for each granular step inside ^^ A common case is testing "pure C#" like math utils, but you can also technically apply it to more game-related features like proper instantiation of a prefab, having some global variable update when the character enters a zone and a callback is run, etc. It's pretty specific to your game, so it's hard to give general advice - but basically, the idea would be to "chop down" your game features in as granular bits as possible, and check each works as intended. Hope it helps a bit, cheers ;)
@ANNYNAWEB
@ANNYNAWEB 2 жыл бұрын
Great Tutorial!
@minapecheux
@minapecheux 2 жыл бұрын
Thanks, I'm glad you like it :)
@thebaltsology246
@thebaltsology246 2 жыл бұрын
This is a great Video I'm Glad I found this. Uhm just beginning to learn how test games. I have some questions, So Unity Test Runner only test the logic of your script? Can you not Test a line on one of your script on Unity Test runner? Hope you make an indepth walk through on this explaining each lines on the test script, even if the video will take an hr or more I'll watch it.
@minapecheux
@minapecheux 2 жыл бұрын
Hi - I'm really glad you liked the video, thanks for your nice comment :) Not sure exactly if it will answer your question, but: the idea with unit testing is to write the actual implementation, the core code, on the one hand, and then write test classes and methods. Those methods contain asserts that are basically boolean checks: if the check passes, the assert passes; else it fails and the test function (we call it a "test case") fails too. So you're not directly testing a line of your main codebase. Rather, you use your codebase to create an example test scenario, and you check that everything goes according to plan with this scenario. I hope it helps a bit? ;) And I'm happy to know you'd be interested in more in-depth tutorials - I will definitely give it a tought!
@thebaltsology246
@thebaltsology246 2 жыл бұрын
@@minapecheux thank you very much, I understand it better now. Well i guess I need to to learn how to use my code base to write test scenario.
@kozavr
@kozavr Жыл бұрын
What you really should have made is create a script, a Monobehaviour class with some methods and make real testing of those methods. Because what you have now have little connection to testing within Unity.
@minapecheux
@minapecheux Жыл бұрын
Hi! So, my goal with this video was mainly to introduce the concept of unit testing and show the main tools to do it in Unity - but I wanted the tuto to remain fairly short, and I decided not to spend too much time on a specific example to keep it somewhat "generalist". I'm sorry if you were looking for more Unity-specific instructions; hopefully, there are other videos on the net that could help you with that ;) Cheers!
@kozavr
@kozavr Жыл бұрын
@@minapecheux "The devil is in the details"
@ragediablo1582
@ragediablo1582 2 жыл бұрын
You are a life saver
@minapecheux
@minapecheux 2 жыл бұрын
Happy you liked it! :)
@kardonov
@kardonov Жыл бұрын
What a wondrerful voice😍
@achrafjarrar433
@achrafjarrar433 Жыл бұрын
Absolutely uselss , I needed the main functions to do testing
@minapecheux
@minapecheux Жыл бұрын
Hi - sorry you didn't find what you were looking for :( This video was about the concepts and main principles of unit testing, and it is pretty hard to show specific examples given how use case-dependent it is... I hope you'll find other resources that help you apply it to your project! Cheers.
@arremm4978
@arremm4978 8 ай бұрын
Why doesn't anyone know how to test their actual project code in Unity? Every resource out there has people writing the code their testing within their test assembly. That makes zero sense.
@mireazma
@mireazma Жыл бұрын
You'll wanna fiddle with the audio of your video, i.e. use an equalizer cause it's barely understandable. That's the reason I disliked it. Use less bass and highs and more center.
@minapecheux
@minapecheux Жыл бұрын
Heya - thanks for your feedback, I'm sorry you had issues with the video's audio :/ That's weird cause no one else has ever told me that... 🤔 that being said, I did improve my audio setup+mixing skills since last year, so it's also partly due to the fact that this video is "old" ;)
@Xblade-Dev
@Xblade-Dev 2 жыл бұрын
Almost good, but unnecessarily complex examples
@minapecheux
@minapecheux 2 жыл бұрын
Hello! Thanks for your interesting feedback :) I'd be curious to get more details on that, if you have some time: how do you find the examples are complex? What would you have preferred instead? This way if I have to explain something similar one day, I can improve and do a better job ;) Thanks, cheers!
@Xblade-Dev
@Xblade-Dev 2 жыл бұрын
@@minapecheux no need for Matrixes and such - just assert foo is true, for example, otherwise the focus is taken away to figure out what you're trying to assert
@minapecheux
@minapecheux 2 жыл бұрын
@@Xblade-Dev Thanks for the details! Honestly, I thought that having sort of a red wire between theory and practice would be better, and that matrices were a good way to show why unit tests can be interesting. (otherwise with just foo is true, you're kinda left wondering why you'd want to check that foo is true... ^^) Also, part of the tutorial was also about showing that knowing what to asset can be difficult, and how we can do it on a specific example. But you're right in the sense that this is not just about "doing unit tests in Unity", it's more of a global introduction to the idea, with a larger scope... Anyway, again - thanks for your comments, cheers!
Make a physics-based cannon [Unity/C# tutorial]
18:11
Mina Pêcheux
Рет қаралды 10 М.
xUnit or NUnit? Picking the Right Testing Library
10:00
Nick Chapsas
Рет қаралды 50 М.
How to treat Acne💉
00:31
ISSEI / いっせい
Рет қаралды 57 МЛН
Smart Sigma Kid #funny #sigma
00:33
CRAZY GREAPA
Рет қаралды 39 МЛН
Мен атып көрмегенмін ! | Qalam | 5 серия
25:41
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 55 МЛН
Test-Driven Development // Fun TDD Introduction with JavaScript
12:55
Thoughts About Unit Testing | Prime Reacts
11:21
ThePrimeTime
Рет қаралды 236 М.
You Can't Unit Test C, Right?
23:09
LinuxConfAu 2018 - Sydney, Australia
Рет қаралды 53 М.
Unit Tests in Unity
11:42
Infallible Code
Рет қаралды 119 М.
Unit Testing C# Code - Tutorial for Beginners
44:56
Programming with Mosh
Рет қаралды 614 М.
Speed Up Compile Times in Unity with Assembly Definitions
9:54
Game Dev Guide
Рет қаралды 93 М.
How to Setup a Project for Testing (Unity Tutorial)
10:56
Infallible Code
Рет қаралды 29 М.
20 Advanced Coding Tips For Big Unity Projects
22:23
Tesseract
Рет қаралды 204 М.
Getting Started with Unit Testing in Visual Studio 2022 - nUnit
16:02
CuriousDrive: Solve Coding Problems & Win Prizes!
Рет қаралды 111 М.
How to treat Acne💉
00:31
ISSEI / いっせい
Рет қаралды 57 МЛН