Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Excellent Unit Testing Video by Roy Osherove

4/18/2016

0 Comments

 
I was basically wandering around on Youtube looking for some videos on Unit Testing to help me with my current project and I stumbled across this video. I've seen some of Roy's other presentations, and I really think this is one of his best ones yet. Most of Roy's books are on .NET so it was nice to see him talking about Javascript. For your reference, here's teh video:

Key Takeaways

- In order work unit testing to work for your project, you must focus on these three things:
  • Trustworthiness
  • Maintainability
  • Readability

- Roy describes unit tests as the "green zone" in that you should never, ever have unit tests that down pass when pulling down the project files and running the tests.

- It's interesting that he say's you shouldn't test private methods. He says that the public methods are the contract and that testing them should be enough. He mentions that sometimes if you really that you should be testing a private method then you probably ought to have it be public.

- He goes on to point out how many js frameworks don't have all their tests passing when you pull the project, and he really hates when there are failing unit tests, and he especially hates when there are failing unit tests and then some says, "don't worry about it because X" since that destroys the trustworthiness of your whole test suite.

- He emphasizes the "no broken windows" principle as it applies to unit tests- if you begin with bad practices or allow others to write tests with bad practices then others will copy them and it will turn into some of the awful examples that he shows in his slides.

- He makes a very clear distinction between unit tests and integration tests, and he mentions many times that he has two distinct "projects"; one for unit tests and one for integration tests. In Angular world these integration tests would be protractor tests, and the best practices for protractor and Jasmine tests match up with that Roy is saying about his integration tests.


My Recommended Angular 1 Unit & E2E Workflow

In terms of project structure, it would be interesting to know what Roy thinks about mine. I use the Gulp-Angular yeoman generator, and I really like their style. Basically, the e2e tests have their own seperate folder on the same level as your src folder, gulp folder, dist folder, etc. Then all of your source code, html, js, scss, images, fonts, sounds, etc, go inside of the src folder. Your unit tests (anything with a spec.js files) are automatically picked up by the trusty gulp scripts. I have a very component-focused directory structure, and I like to  throw the unit tests right into the component folder with all the other source files. This helps me 1) to be able to find the tests easily, 2) to actually write a test- create a describe full of it's and fill it with useful test for that component, and 3) to always have a test that "goes with" a js file. What do I mean by this? For my file naming conventions I do all lowercase dashes separating words in the name, then a dot, and finally a suffix that corresponds to that type of Angular 1 component it is. So for example, I might create files called prize-generator.service.js or left-menu.controller.js, and I would be sure to create a prize-generator.service.spec.js and left-menu.controller.spec.js file. I really like having them in the each same folder, sitting directly next to each other, holding hands, laughing, having fun together. This way you can easily move back and forth, it makes sense since they are only using things in each other's classes. If you did not make a file for each js file, you are either not writing tests at all or putting some in the same file, in which case you'd want to refactor that out at some point anyway for separation of concerns. These are run on the command line with gulp test. My protractor tests, even though I don't 100% "like it", have a parallel source structure in the e2e folder I was talking about. These are also .spec.js files, but gulp knows they are protractor tests because they are in the e2e folder. They can also have .po.js files, interact with DOM by calling click events and keyboard inputs, and make real requests to your async calls. 


If you have a testing directory structure that works for you or have comments on Roy's presentation, let me know in the comments. :)
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Picture
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    March 2023
    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2023