"The Clean Code Talks -- Unit Testing"

Поделиться
HTML-код
  • Опубликовано: 4 окт 2024

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

  • @Abdullah-mg5zl
    @Abdullah-mg5zl 8 лет назад +147

    Here is a summary of the talk:
    -we can test at different levels
    -unit testing is testing at the class level
    -functional testing is testing at the "group of classes" level (you want to see if a group of classes work together properly)
    -scenario testing is testing at the "application" level (does the application behave correctly as a whole)
    -in order to unit test a class, that class must be isolateable (that way, if the test fails, you know exactly who's fault it was)
    -in order for a class to be isolateable, all of its dependencies must be passed in its constructor (the class itself must not "new" (create) any instances of the classes it depends on)
    -when testing a certain class, you can pass in stub objects, mock objects, or friendly objects to its constructor to satisfy its dependencies
    -whether you pass in stub, mock, or friendly objects, if the test fails, you KNOW its not b/c of the passed in dependencies, therefore it must be the fault of the object being tested
    -if you code so that your class gets all of its dependencies in its constructor, then you are doing "dependency injection"
    Hope this is helpful to someone :)
    Thank you to the presenter for such an informative and great presentation and thank you GoogleTechTalks for the upload!
    Very useful video!

    • @BrianKTran
      @BrianKTran 7 лет назад +3

      Concise and helpful. Thanks!

    • @Abdullah-mg5zl
      @Abdullah-mg5zl 7 лет назад +1

      No problem :)

    • @MrSksporky
      @MrSksporky 7 лет назад +2

      Thanks for the great summary!

    • @Abdullah-mg5zl
      @Abdullah-mg5zl 7 лет назад +1

      Not a problem, glad you found it helpful! :)

    • @SpMeKP
      @SpMeKP 6 лет назад +1

      Compact and essential. Thank you so much!

  • @FloppyDobbys
    @FloppyDobbys 8 лет назад +31

    Watching this 8 years in the future. Still very helpful

    • @MegaStegger
      @MegaStegger 7 лет назад +3

      Six months later... Still very helpful :-)

    • @l_combo
      @l_combo 5 лет назад +2

      2 years later ;)

    • @blasttrash
      @blasttrash 4 года назад +5

      11 years later lol

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

      Hi there from 2020

    • @WarrenParks
      @WarrenParks Год назад +2

      2023 here. Going to present this to our team tomorrow.

  • @macdx2815
    @macdx2815 9 лет назад +21

    "Dependency Injection is asking for things in the constructor" I like that sentence

    • @sethtaylor7519
      @sethtaylor7519 6 лет назад +1

      ** "Dependency Injection is passing in ALL class dependencies through the constructor" **

  • @sutikshnadubey
    @sutikshnadubey 11 лет назад +10

    Not just talks, after 3-4 years of this talk, he came up with AngularJS framework so that we can have Dependency Injection in JavaScript too, so that we don't have to rely on Monkey patching only. WoW. :)

  • @rockYhre
    @rockYhre 10 лет назад +2

    This is a great talk from great programmer. I fully recommend it if you aren't familiar with the topic yet.

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

    Speaker is the founder of Angular!! did you know?

  • @IgnatRemizov
    @IgnatRemizov 8 лет назад +14

    For the subtitles where is says [INDISTINCT],
    12:23 "tests that, that... that verify that the piston is the uh of the correct shape, that the oil is present,"
    30:30 "And genuinely, it's a lot less intrusive of a framework than a DI framework."
    30:40 "Of course, Dave is the uh, the Ruby guy who's the highly-uh dynamic language guy, I'm the static type of guy."
    31:00 "HEVERY: Aah, okay. So it keeps track of it and then cleans it up when it's done. OTHER (Same time): And it keeps track of everything, and restores everything when it's done. "

  • @TheDerravaragh
    @TheDerravaragh 13 лет назад +1

    Excellent talk. Very concise and intuitive examples.

  • @ChevailerNoir
    @ChevailerNoir 14 лет назад

    Some great information. I like that you talk about things that aren't emphasized in school.

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

    Excellent talk. Very concise and intuitive examples.....

  • @hasallie74
    @hasallie74 10 лет назад +5

    great tut!

  • @stewie1571
    @stewie1571 10 лет назад +18

    Practice TDD! I'm surprised TDD wasn't really brought up in this video. TDD more fully covers all of the topics discussed here (such as dependencies / decoupling / mocking / faking, unit testing, coverage etc). "Uncle Bob" (Robert C. Martin) has many good talks about how to practice TDD and the benefits of it here on RUclips. FYI, TDD will likely seam stupid at first - I thought so, but now I love it. And when practiced with S.O.L.I.D. principles and perhaps some good standard design patterns, it seems to encourage a good architecture too. You'll likely end up with a good Onion, Hexagonal, Ports & Adapters (whatever you want to call it) architecture.

    • @LordLarzuk
      @LordLarzuk 10 лет назад

      I'm trying to find those videos where Uncle Bob talks about TDD practices, but I just can't find em, can you pass me URL ?
      Thanks in advance

    • @LordLarzuk
      @LordLarzuk 10 лет назад

      Thank you

    • @oviiarmaghan5552
      @oviiarmaghan5552 8 лет назад

      +Stewart Anderson can u share the URL here ?

    • @stewie1571
      @stewie1571 8 лет назад

      I would still take his advice with a grain of salt. Some would say that you need end to end tests (not unit tests) to approve the project to ship (but maybe that was what he was talking about - idk - I've not watched this in a long time). Many would say that you don't ever expose internals even to tests. I would agree. Everything should still be encapsulated but without loosing coverage and while keeping the tests DAMP and simple. Doing this should help the design refactor into standard design patterns and SOLID principles (which is an acronym I believe Uncle Bob started). Achieving good encapsulation with perfect coverage of all logic/behavior should also help the design with cohesion as well and obviously decoupling the code at the same time.

  • @bradfach
    @bradfach 10 лет назад

    Excellent video... sums up the need for unit testing, dependency injection :)

  • @senihokur
    @senihokur 11 лет назад +4

    back when I watched this first time, I thought testing a car was just an analogy

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

    Summary:
    How do you write hard to test code?
    1. Mixing new with logic
    2. Looking for things
    3. Work in the constructor
    4. Global state
    5. Singletons
    6. Static methods
    7. Deep Inheritance
    8. Too many conditionals
    9. Mixing Service/Value
    10. Mixing Concerns
    [It is difficult to test procedural code. Because in order to test we need to isolate something. In OOPS, this is done by polymorphism which is not present in procedural code.]
    [We are tied to the inheritance in run time and there is no flexibility. If we want to test class X, which inherits from other classes like A, B, C, D, E, F, G; it is hard to test X. We are also testing these other classes if we do that. No separation.]
    Writing testable code:
    1. Good OO
    2. Dependency Injection
    3. Test-Driven Development
    4. Knowledge about untestable code
    [There is no secret to writing tests but there is to writing testable code]
    Progression of Testing
    - Scenario/Large Tests
    - Tests the whole application by pretending to be a user
    - It can be a little slow and flaky
    - More execution time
    - Functional/Medium Tests
    - External dependencies are simulated
    - Tests collection of classes as subsystems
    - Medium execution time
    - Unit/Small Tests
    - Tests individual classes/methods in isolation
    - Very fast and doesn't normally need a debugger
    - Less execution time
    It is difficult to do unit testing of a class because class generally has dependencies and those classes have further dependencies. These intertwined networks of dependent classes make it difficult to test individual class.
    If it is a leave class, it is easy. But it is difficult to test a class slightly up the hierarchy. To do this we need to cut the dependencies or draw a line between the class and its dependencies.
    We could do this by many methods:
    - We can use the actual class that we trust that we have tested already that doesn't throw an error.
    - We can use a stub that may simulate the behaviour of existing code.
    - We can also mock the class.
    How can we separate these things or draw the line?
    We can separate Object Graph Construction & Lookup( i.e. constructing the application with all the instances of classes) from the Business logic. This makes testing easy.
    Practical example being: Rather than instantiating the dependent classes in the constructor of the class being put under test, pass the instantiated classes directly as arguments to the constructor. Now we are free to do this instantiation in the tests and pass it to the class being tested.
    If this is not done, we can't instantiate in isolation and in testing, we will be just mimicking the whole application. This would make unit testing scenario-based testing. Hence, the new operator should never be mixed with logic.

  • @InnerFood
    @InnerFood 12 лет назад +1

    agree, a lot of literature covers topic, like "why procedural code is much easier to test". If you have a static method with big, unmockable side effects, than this code is bad by procedural standards. Pure procedural functions don't have side effects which is golden grail for testing.

  • @jesperheilmann
    @jesperheilmann 11 лет назад +1

    Constructor paradigm:
    Ask for needed objects (by parameters)
    Do not instantiate dependencies.
    (Separating Object graph construction from business logic)

  • @D3mi4n
    @D3mi4n 16 лет назад

    This should be part of some kind of "Google academy for software developers"
    A kind of virtual course for student with, and without, CS (or related) degrees.

  • @temper9775
    @temper9775 15 лет назад

    Great video!

  • @lucasdesouza3658
    @lucasdesouza3658 5 лет назад +1

    Static’s are fine if they are purely utility or functional in nature so they don’t really hurt your tests.

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

      The problem comes when they start referencing other dependencies

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

      Clement Osei Tano correct.

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

      What if I need them for performance reasons or to prevent memory usage/heap fragmentation (which can be troublesome on embedded hardware)?

  • @promiseomiponle810
    @promiseomiponle810 4 года назад +5

    can someone drop the timestamp for where he explains what a seam is?

  • @devnami
    @devnami 10 лет назад

    A good lecture on Testing :)

  • @succelus
    @succelus 16 лет назад

    Keep the good Talks !

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

    I am shocked that DI existed in 2008. I thought it was fairly recent stuff. 😅

  • @mausolo1963
    @mausolo1963 7 лет назад

    Much appreciated

  • @JonathanHartley
    @JonathanHartley 16 лет назад

    Excellent, very insightful. I've been enjoying Misko's essays for a while now.
    However, in dynamic languages, I don't think it is true that static method calls cannot form a seam for testing. In Python, for example, a static method can be patched out temporarily on either the current module, or another module, for the duration of the test. We use this technique at my work all the time for testing.

  • @bhavikprulogics
    @bhavikprulogics 11 лет назад

    "How do we write hard to test code? We mix object creation code with business logic. This will assure that a test (test case) can never construct a graph of objects (object graph - mocks, fakes, stubs, proxies) different from production. Hence nothing can be tested in isolation."

  • @neohubris
    @neohubris 15 лет назад

    great talk!

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

    If tests are flaky, then there's probably a problem understanding the system.
    If the tests are slow, then there's probably a failure to leverage concurrency/parallelism.
    These two issues are not independent...

  • @deepakbala11
    @deepakbala11 11 лет назад

    check for conversation display.... unit testing

  • @maksuree
    @maksuree 6 лет назад

    what can i take from this as a novice programmer who's still writing simple, single-script python programs? what habits should i start now to ensure i write my code to be testable as my programs get more advanced with experience?

  • @juliopari
    @juliopari 11 лет назад

    Good

  • @JohanZahri
    @JohanZahri 14 лет назад

    when he mentioned about the evil of global state, i almost instatly thought of NWO.. :))
    I think that's an analogy for the human anthropological level, and it's definitely untestable :P

  • @adg16610
    @adg16610 13 лет назад

    code quickly becomes hard to test if it is written .... _before_ the unit tests ;)

  • @ViktorLofgren
    @ViktorLofgren 11 лет назад

    You have amazing English for an 1-year old.

  • @Obzenner
    @Obzenner 9 лет назад +1

    5:54 Martin Odersky :)

  • @anhvuong5330
    @anhvuong5330 11 лет назад

    It's good for every body #testervn

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

    TIL about the word called monkey patch

  • @ryanjackson0x
    @ryanjackson0x 9 лет назад

    many (complex) dependencies

  • @ryanjackson0x
    @ryanjackson0x 9 лет назад

    More side effects

  • @deepakbala11
    @deepakbala11 11 лет назад

    Post from shared Message.... !@# diff

  • @anhvuong5330
    @anhvuong5330 11 лет назад

    Testing ...#qcvietnam

  • @haridaniel777
    @haridaniel777 11 лет назад +1

    Ha ha! no audience!! poor lazy guys

  • @CanMetan
    @CanMetan 9 лет назад +1

    I HATE unit testing.
    Unless you're programming an API, I can't understand any reason why you'd do that....

    • @eNSWE
      @eNSWE 9 лет назад +4

      +Kazathul why would you NOT want 100 % coverage of every single line of code, resulting in formal executable documentation, a drastic reduction in need for debugging and reducing the risk of refactoring to almost 0?

    • @CanMetan
      @CanMetan 9 лет назад

      eNSWE Reduncancy and waste of time. Again, if we're programming an API it makes sense. Cuz then the end user will use every possible function in there so it's helpful.
      However for regular programs, I like to prepare test cases for the functionalities (top most hierarchy for the program) and white test all possible inputs and their results. Some functions I test the input before passing in to various functions. So if I unit test, they will fail. Where in the real program they never will. I hope I've explained well.

    • @eNSWE
      @eNSWE 9 лет назад +6

      I'm sorry but it really sounds like you don't know what unit tests are and what the point of them is. not saying that to sound elitist or to bring you down, but you really missed the point. check out a book or two on the subject (try "the art of unit testing" by roy osherove or "test-driven design: by example" by kent becket).

    • @CanMetan
      @CanMetan 9 лет назад +1

      eNSWE I should check them out.

    • @iamjustsobody3991
      @iamjustsobody3991 8 лет назад +1

      +eNSWE it's "test-driven development" by kent beck, awesome book

  • @dashby4716
    @dashby4716 6 лет назад

    Wow. This presenter talks as if this is all testing is, but hes missed out all of the investigative side of testing (e.g exploratory testing approach). I am dubious that this guy is even aware of the investigstive side of testing, since he's only talking about checking expectations so much... What about risks that you dont have an expectation for, therefore cant assert? You need to explore those risks.
    I hope the presenter learns about the whole picture of tesring soon, since hes presenting to a LOT of peaople to teach them... And only teaching them half of what testing is potentially really damaging.

  • @din123z
    @din123z 10 лет назад

    Great talk!

    • @dinsejal1
      @dinsejal1 9 лет назад

      good for programmers

    • @dinsejal1
      @dinsejal1 9 лет назад

      the word deterministic is used. You do not comment on that.