亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Using EclEmma to Write Better Unit Tests

發布時間:2020-08-04 20:38:43 來源:ITPUB博客 閱讀:153 作者:thunderbird2 欄目:編程語言

轉載:http://ericanderson.us/2008/05/08/using-eclemma-to-write-better-unit-tests/

If you don’t already write unit tests you should be. (Hey, why aren’t you writing unit tests?) Unit testing has so many benefits and the upfront developer cost to write some unit tests can pay huge dividends later when you aren’t spending time debugging broken code nor attempting to save face due to clueless mistakes. You can also use them as a contract to the expectations of your implementations.

[@more@]

So, assuming you have some unit tests, how useful are they if they don’t test everything? At some level you will want to have a good idea that you’re testing everything. (NOTE: I mean everything really important. Writing perfect 100% coverage like this would likely be too expensive for the entire codebase.) This is where Emma comes in (and more importantly for us, EclEmma, an Eclipse plugin for Emma). Emma is a code coverage tool which lets you visual which parts of your code get executed during some execution (regular or JUnit).

Lets walk through using EclEmma to ensure that we have adequate testing being done. Lets test the following two classes.

GuessTheNumber.java:

view sourceprint?
01.import java.util.Random;
02.
03.public class GuessTheNumber {
04.
05. private final Integer value;
06. private final int min;
07. private final int max;
08. private int guesses = 0;
09. private boolean solved = false;
10.
11. public GuessTheNumber(int min, int max, int value) {
12. this.min = min;
13. this.max = max;
14. this.value = value;
15. }
16.
17. public GuessTheNumber(int min, int max) {
18. this(min, max, getRandomNumber(min, max));
19.
20. }
21.
22. public int guess(int i) {
23. if (solved)
24. throw new IllegalStateException();
25.
26. guesses++;
27.
28. int ret = value.compareTo(i);
29.
30. if (ret == 0)
31. solved = true;
32.
33. return ret;
34. }
35.
36. public void resetCount() {
37. guesses = 0;
38. solved = false;
39. }
40.
41. public int getValue() {
42. if (solved == false)
43. throw new IllegalStateException();
44.
45. return value;
46. }
47.
48. public int getMin() { return min; }
49. public int getMax() { return max; }
50.
51. private static int getRandomNumber(int min, int max) {
52. return new Random().nextInt(max - min + 1) + min;
53. }
54.}

And SequentialStrategy.java:

view sourceprint?
01.public class SequentialStrategy {
02.
03. private final GuessTheNumber guesser;
04.
05. public SequentialStrategy(GuessTheNumber guesser) {
06. this.guesser = guesser;
07. }
08.
09. public int solve() {
10. for (int i=guesser.getMin(); i< =guesser.getMax(); i++) {
11. if (guesser.guess(i) == 0)
12. return i;
13. }
14.
15. throw new IllegalStateException();
16. }
17.}

Finally, we’ll write up a basic StrategyTest.java:

view sourceprint?
01.import junit.framework.TestCase;
02.
03.public class StrategyTest extends TestCase {
04.
05. public void testSequentialStrategy() {
06. GuessTheNumber guesser = new GuessTheNumber(5, 100);
07. SequentialStrategy strategy = new SequentialStrategy(guesser);
08.
09. int value = strategy.solve();
10. assertEquals(guesser.getValue(), value);
11. }
12.}

So the question is, how good is our test? If you installed EclEmma, you can right click on your Unit Test in Eclipse, head to “Coverage As” -> “JUnit Test”

Using EclEmma to Write Better Unit Tests

This will run your unit test against your code and when complete, highlight the code green, yellow, or red for covered, partially-covered, and not-covered respectively.

According to EclEmma, our code coverage when running StrategyTest is:
Using EclEmma to Write Better Unit Tests

Thats not too bad, lets take a look at the output for SequentialStrategy:
Using EclEmma to Write Better Unit Tests

We may want to test the IllegalStateException since we currently don’t and we are expecting it to happen if we’ve guessed everything between min and max and haven’t solved the problem. This would ensure if someone else comes in behind us and changes this code, the unit test will fail if they take that out and change it to return, say, Integer.MIN_VALUE.

Lets also take a look at some of GuessTheNumber:
Using EclEmma to Write Better Unit Tests

It appears we also want the contract to include an IllegalStateException being thrown if you have already solved the guessing game. It also looks like we don’t ever call resetCount() and possibly retest after doing that. We also never check if getValue() fails prior to having a solution.

If we modify the test slightly to:

view sourceprint?
01.import junit.framework.TestCase;
02.
03.public class StrategyTest extends TestCase {
04.
05. public void testSequentialStrategy() {
06. GuessTheNumber guesser = new GuessTheNumber(5, 100);
07. SequentialStrategy strategy = new SequentialStrategy(guesser);
08.
09. for (int i=0; i<3; i++) {
10. try {
11. guesser.getValue();
12. fail("An exception should have been thrown");
13. }
14. catch (IllegalStateException ex) { /**/ }
15.
16. int value = strategy.solve();
17. assertEquals(guesser.getValue(), value);
18.
19. try {
20. guesser.guess(guesser.getMax() + 1);
21. fail("Shouldn't reach this point");
22. }
23. catch (IllegalStateException ex) {/**/}
24.
25. guesser.resetCount();
26. }
27. }
28.
29. public void testBadGuessGame() {
30. GuessTheNumber guesser = new GuessTheNumber(1, 10, 3000);
31. SequentialStrategy strategy = new SequentialStrategy(guesser);
32.
33. try {
34. strategy.solve();
35. fail("Should not reach this point");
36. }
37. catch (IllegalStateException ex) { /**/ }
38. }
39.}

And now our code coverage is:
Using EclEmma to Write Better Unit Tests

I know this was a very basic example that really didn’t test any complex logic or conditions, but I hope it gives you a good idea of how you can use code coverage to improve your unit tests.

  • Rant: Using Error Codes in User Messages »
  • « Consistent Hashing
  • -->
    向AI問一下細節

    免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

    AI

    景东| 承德县| 石台县| 崇阳县| 成安县| 河北区| 抚宁县| 田林县| 石台县| 思南县| 平原县| 阜康市| 交城县| 武穴市| 巴中市| 彰化市| 古蔺县| 罗城| 六安市| 枣阳市| 织金县| 霍城县| 丰原市| 鸡泽县| 彭阳县| 娄底市| 罗田县| 商水县| 开原市| 读书| 边坝县| 民权县| 平度市| 洪泽县| 清河县| 双牌县| 吉林省| 巴南区| 延吉市| 霍林郭勒市| 榆中县|