Have a nice smile
"The friend is sick, want to hang brine." Give him the needle is an internship nurse, tied for half a day did not plunge into the blood vessels.
He grinning the pain, but he called the head nurse.
The head nurse good technique, saw her sharply in the blood vessel, then immediately pulls out,
Pass the needle to the intern nurse and say, "Have you seen it clearly?" You try again! ”】
Ask questions
How to write the test code gracefully???
solve the problem
1) TDD (test-driven development), which means to write unit tests first, then write the corresponding code, and modify the debugging to let the written code pass the unit test. With TDD, the test will cover all the code, and the ratio of the test code to the production code is likely to reach 1:1, so it's also a cost issue, all of which we want to keep the test neat.
2) Benefits of unit testing: Allow code to be extensible, maintainable, and reusable.
3) Three elements of neat testing: readability, readability, readability.
4) Each test can be divided into three links: the construction of test data, operation of test data, check whether the operation to achieve the desired results.
5) Double standard: Some conditions in the test environment and in the production environment do not have to be fully consistent. Performance issues such as memory and CPU are sometimes considered in a production environment, and these restrictions are not required in a test environment.
6) A test of an assertion that does not have to be completely tangled, but the number of individual test assertions should be minimized, testing only one concept, or the problem of a single duty;
7) F.I.R.S.T principle
F fast: The test needs to run frequently, so it should be able to run quickly;
I Independent: Tests should be independent of each other;
R repeatable: The test should be able to repeat in any environment;
S self-validating: Self-sufficiency verification, test should be able to see the results of success or not;
T timely: Tests should be written in a timely manner and should be written just before the production code;
Here is an example to illustrate the above theory (the theory is very boring, the example is very good):
Package Com.hwy.test;import Org.junit.assert;import Org.junit.test;import java.util.arraylist;import java.util.List ;p Ublic class Codecleantest {@Test public void Testbuysnacks () {list<string> snacks = Buysnacks (); Assert.assertequals ("There is a problem with buying snacks, please check!!!", True,snacks.size () >0); } @Test public void Testeatsnacks () {/** This is a snack data that we build ourselves, and there are definitely no problems with nulls **//** We don't call the Buysnacks method, because it's a single The question of responsibility is to test only a concept **/list<string> snacks = Getsnacks (); Assert.assertequals ("Not eating together 3 snacks", 3, Eatsnacks (snacks)); } @Test public void Testdroplitter () {list<string> snacks = Getsnacks (); Assert.assertequals ("Rubbish is not thrown away!!!", true,droplitter (snacks)); } @Test public void Testdatewithgirl () throws exception{Assert.assertequals ("Appointment failed", true, Datewithgirl ()); }/** * Build snack data (The fact that these comments are not needed, just for your understanding) * @return */public list<string> getsnacks () {list& Lt string> snackslist = new ArRaylist<> (); Snackslist.add ("milk"); Snackslist.add ("Chocolate"); Snackslist.add ("potato chips"); return snackslist; }/** * Buy snacks (The fact that these comments are not needed, just for everyone to understand) * @return */public list<string> buysnacks () {LIST<STR ing> snackslist = new arraylist<> (); Snackslist.add ("milk"); Snackslist.add ("Chocolate"); Snackslist.add ("potato chips"); /** is intentionally shown here as an empty **///return null; return snackslist; }/** * Appointment */public boolean datewithgirl () throws Exception {Boolean issuccess = false; list<string> snackslist = Buysnacks (); /** use reverse thinking, throw a business exception, here I just use simple exception instead of **/if (null = = Snackslist | | snackslist.size () ==0) {throw new Exception ("You don't have a snack or a snack you bought is a problem, please check!") "); The/** code executes to this step to show that snackslist is not NULL, and all subsequent * operations do not have to determine whether snackslist is empty **/list<string> litter = Eat Snacks (snackslist); Droplitter (litter); Issuccess = true; return issuccess; }/** * Snacking (the fact that these comments are not needed, just for everyone to understand) * @param snacks * * * public list<string> eatsnacks (list<string& Gt Snacks) {for (String snack:snacks) {System.out.println ("eat together" + snack); } return snacks; }/** * Still rubbish (the fact that these comments are not needed, just for everyone to understand) * @param litter */public boolean droplitter (list<string> litter ) {Boolean isdrop = false; for (String snack:litter) {System.out.println ("Discard Trash:" + snack); }/** here deliberately write a bit redundant, just convenient for everyone to learn **/Isdrop = true; return isdrop; }}
Each of the above test methods is: Test + the original method name, the general test method is so named. At the same time, the above test methods, not to the method are independent, testing only a concept.
Here is an example of the error:
Package Com.hwy.test;import Org.junit.assert;import Org.junit.test;import java.util.arraylist;import java.util.List ;p Ublic class Codecleantest {@Test public void testbuysnacks () throws exception{/** like this, in a test method, testing too many methods, creating The test case is difficult to reuse **//** this does not conform to the rules of one assertion per test, and does not conform to the F.I.R.S.T principle **/list<string> snacks = Buysnacks (); Assert.assertequals ("There is a problem with buying snacks, please check!!!", True,snacks.size () >0); Assert.assertequals ("Not eating together 3 snacks", 3, Eatsnacks (snacks)); Assert.assertequals ("Rubbish is not thrown away!!!", true,droplitter (snacks)); Assert.assertequals ("Appointment failed", true, Datewithgirl ()); }/** * Build snack data (The fact that these comments are not needed, just for your understanding) * @return */public list<string> getsnacks () {list< string> snackslist = new arraylist<> (); Snackslist.add ("milk"); Snackslist.add ("Chocolate"); Snackslist.add ("potato chips"); return snackslist; }/** * Buy snacks (The fact that these comments are not needed, just for everyone to understand) * @return * * public list<string> buysnacks (){list<string> snackslist = new arraylist<> (); Snackslist.add ("milk"); Snackslist.add ("Chocolate"); Snackslist.add ("potato chips"); /** is intentionally shown here as an empty **///return null; return snackslist; }/** * Appointment */public boolean datewithgirl () throws Exception {Boolean issuccess = false; list<string> snackslist = Buysnacks (); /** use reverse thinking, throw a business exception, here I just use simple exception instead of **/if (null = = Snackslist | | snackslist.size () ==0) {throw new Exception ("You don't have a snack or a snack you bought is a problem, please check!") "); The/** code executes to this step to show that snackslist is not NULL, and all subsequent * operations do not have to determine whether snackslist is empty **/list<string> litter = Eat Snacks (snackslist); Droplitter (litter); Issuccess = true; return issuccess; }/** * Snacking (the fact that these comments are not needed, just for everyone to understand) * @param snacks * * * public list<string> eatsnacks (list<string& Gt Snacks) {for (String snack:snacks) {System.out.println ("Eat together" + SNACK); } return snacks; }/** * Still rubbish (the fact that these comments are not needed, just for everyone to understand) * @param litter */public boolean droplitter (list<string> litter ) {Boolean isdrop = false; for (String snack:litter) {System.out.println ("Discard Trash:" + snack); }/** here deliberately write a bit redundant, just convenient for everyone to learn **/Isdrop = true; return isdrop; }}
Reading Comprehension
From the poor Charlie's Book of Wisdom
- Busy people seldom visit, and boiling pots never fall into flies.
- When you are frustrated, no one knows, and when you are proud, you don't know yourself.
- Anger always has a certain reason, but there are few good reasons.
- You may sometimes make a big mistake because you think you are always right.
- 100 thieves, also can not steal a person's face, especially when this person has no face.
- People often see themselves wrong, but seldom forget about themselves.
- The quarrel that no one admits is bound to continue endless.
- If your secret does not want to be known to the enemy, please keep it secret from your friends first.
- The lesson of one or two wisdom is better than the knowledge of a pound of books.
other
If there is a little bit of happiness for you, let happiness continue to pass, welcome reprint, Praise, top, welcome to leave valuable comments, thank you for your support!
Elegant programming So write test cases, you are "normal"!