testing - cs361fall2018.github.io · automated testing testing provides a consistent mechanism for...

33
Testing Tuesday, October 9 1

Upload: others

Post on 24-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

TestingTuesday, October 9

1

Page 2: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Announcements

Bug fix to the front end; see #announcements channel for fix

Sprint 1 due Friday at 5.

2

Page 3: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Testing

How do you know your code will work?

If it doesn't work, how do know when you fixed it?

You can manually check the program each time.

Expensive, slow, and unrealistic

3

Page 4: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Automated testing

Testing provides a consistent mechanism for checking that your code meets all requirements.

Testing is automated, so you can continuously check that your code is "up to spec."

4

Page 5: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Testing

We'll only focus on unit-testing.

CS 362 goes in-depth into testing.

5

Page 6: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Verification vs Validation

Validation: are we building the right product?

Acceptance testing, user demos, beta testing

Verification: are we building the product right?

Formal methods, unit tests, regression tests

6

Page 7: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Types of testing

Development testing: done during development, by developers and testers.

Release testing: done prior to a release or other milestone. Done outside the dev team (e.g. the customer)

User testing: done by the actual users (beta testing)

7

Page 8: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Unit testing

“Unit tests test individual units (modules, functions, classes) in isolation from the rest of the program.”

- Kent Beck

8

Page 9: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

JUnitIn Maven, by default, tests live under src/test/java

Each production class has a test class, named *Test

e.g. Game -> GameTest

Test classes live in the same package as production classes

You can access package protected method or fields

9

Page 10: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Test methods

All test methods are annotated with @Test

@Test public void testSomething() { // … }

10

Page 11: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Test fixtures

If you need some set up, annotate the method with @Before. This executes before the each test case.

@Before public void setUp() { }

This allows you to set up test fixtures. A fixture is the context in which a test is run.

11

Page 12: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Test fixtures

To release resources after a test, annotate the clean-up method with @After.

@After public void tearDown() { }

12

Page 13: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Test fixtures

@BeforeClass and @AfterClass allow you to perform text fixture setup and teardown only once per class run

@BeforeClass is executed once, before any of the test methods in the class are run

@AfterClass once after all the tests are run).

13

Page 14: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Assertions

Allow you to check for expected values.

org.junit.Assert class contains assert methods that you can use: assertTrue, assertFalse, assertEquals, etc.

assert is a Java keyword and will crash the JVM if it fails (similarly to C/C++). They are also disabled by default.

14

Page 15: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Test executionJUnit executes tests in a deterministic, but unpredictable order.

Don't rely on the execution order for your tests to pass.

This breaks the independence assumption of unit tests

It might not work on a different JVM, or even on a different run

15

Page 16: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

How do we write tests?

Blackbox testing: You don't know the implementation

Whitebox testing: You know the implementation

16

Page 17: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Blackbox testing

You don't know the structure of code (it's a black box, you can't see inside)

Tests are derived from the specification, documentation etc.

Useful for testing 3rd party libraries as well

17

Page 18: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Equivalence class partitioning

Partitioning the input into "equivalence classes" allows to reasonable cover a good range of the input

e.g. validation for the username field: length between 1 and 25 characters, with no spaces

18

Page 19: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

ECPusername: length = [1, 25] with no spaces

19

Input Output

"" Invalid

"averylongusernamethatislongerthan25characters" Invalid

"username" Valid

"with space" Invalid

"averylongusernamewitha spaceandmorethan25characters" Invalid

Page 20: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Boundary TestingSimilar to ECP, but focuses on edge cases.

For the username validation:

20

Input Output

"" Invalid

"a" Valid

"25characters1234567890123" Valid

"26characters12345678901234" Invalid

Page 21: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Whitebox testing

You know the structure of the code being tested.

The goal is execute all lines of the code, branches, etc.

21

Page 22: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Measuring testedness

How good are you tests?

Statement/line coverage

Branch coverage

Path coverage

22

Page 23: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Statement/line coverage

Measure how many (executable) statements did you cover?

Expressed as a % (number of covered statements / all statements) at different levels: method, class, package, system.

Built into IntelliJ IDEA.

23

Page 24: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Statement/line coverage

Does 100% line coverage guarantee a bug free code?

No!

You can cover all lines, but not cover all branches.

24

Page 25: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Branch coverage

Make sure that all branches in a program are covered.

All decisions points must be exercised.

25

Page 26: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Path coverage

Makes sure that all possible executions paths are covered

Impossible for programs with loops

Is our program bug free?

No. How does our component interact with others?

26

Page 27: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Integration testing

Test that your system's components interact as expected

Can reveal more subtle bugs. A failed integration test is a missing unit test.

You can expose the bug by writing a unit test on the right component.

27

Page 28: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Regression testing

Tests to make sure that bugs reappear.

For every bug, first write a test to expose the bug (fails), then fix.

The test is now part of your regression suite.

28

Page 29: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

User testing

The users just use the system

This allows you to check that everything works as the users expect to.

It also exposes incomplete features, missing/misunderstood requirements etc.

29

Page 30: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Assertions

Your tests are only as good as your assertions!

Weak assertions will let bugs through.

30

Page 31: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Bad

@Test public void testAttack() { // ..some setup code.. board.attack(3, 'A'); }

31

Page 32: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

OK…

@Test public void testAttack() { // ..some setup code.. Result r = board.attack(3, 'A'); assertNotNull(r); }

32

Page 33: Testing - cs361fall2018.github.io · Automated testing Testing provides a consistent mechanism for checking that your code meets all requirements. Testing is automated, so you can

Good

@Test public void testAttack() { // ..some setup code.. Result r = board.attack(3, 'A'); assertEquals(AttackStatus.HIT, r.getResult());

}

33