functionality and prove it correct Each test unit must be fully independent Tests should be logically as simple as possible Ensure that tests run fast Write tests for parts with fewest dependencies on external resources first, and work your way up Nothing is private: Test every private method unless they correspond to a trivial implementation detail Thumbs Up by Andrew Walsh
long and descriptive names for testing functions sqrt vs test_001_square_of_negative_number_raise_value_error Use the appropriate assertion assertTrue / assertLess / assertIsInstance / assertDictEqual Order of arguments assertEqual(expected, actual) Mock with parsimony Third-party lib / HTTP call over network (mailer, api) / Randomness / heavy-load non-relevant part of code Test realistic edge cases test_is_prime({"toto": [1, 2, 3]}) vs test_is_prime(0) Unittest Best practices
input Selenium WebBrowser FW to drive the browser def test_known_user_not_logged(self): go_to_recruiter_dashboard() enter_whitelisted_email() click_get_started() click_connect_with_FB() facebook_login(switch_standalone=True) see_the_email_verification_banner() access_sjs() see_sjs_onboarding() # go back to the landing page go_to_recruiter_dashboard() see_the_email_verification_banner()
= dealer.get_meth() except ValueError: self.drug = dealer.get_coke() else: self.drug = self.go_to_church() self.drug.use() Hot Coverage (sed "s/Co/Be/") try to go through all possible paths Code can be 100% covered yet not fully tested Mock might make miss some cases > use_drug_from("Heisenberg") > use_drug_from("Pablo Escobar") > use_drug_from("GrandMa") > use_drug_from(None) > with patch.object(o, "get_dealer") as mock > mock.return_value = Dealer("Heisenberg")
realistic edge cases Don’t merge code that is not tested Test act as a safeguard when refactoring But don’t test too much the underlying implementation else you’ll refactor as much tests as code Keep in Mind