Methods 4. - PowerPoint PPT Presentation

Methods 4 l.jpg
Download
1 / 14

Methods 4. 0. Take in assignment 5. 1. Review. 2. Stubs and drivers. 3. Unit testing and integration testing. 1.Review. (1) Class-wide versus local variables: what is the distinction? (2) What are local variables? Local to what? (3) Where can local variables be accessed?

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Presentation

Methods 4.

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Methods 4 l.jpg

Methods 4.

0. Take in assignment 5.

1. Review.

2. Stubs and drivers.

3. Unit testing and integration testing.


1 review l.jpg

1.Review.

  • (1) Class-wide versus local variables: what is the distinction?

  • (2) What are local variables? Local to what?

  • (3) Where can local variables be accessed?

  • (4) Where can class-wide variables be accessed?


Why use l.jpg

Why use…

  • Local variables?

  • Class-wide variables?

  • When is NOT a good idea to use a local variable?

  • When is it NOT a good idea to use a class-wide variable?


Why does c ban global variables l.jpg

Why does C# ban global variables?

  • What is the danger of global variables?

  • Why do we pass parameters?

  • How can we avoid excessive parameter passing?


2 stubs and drivers l.jpg

2. Stubs and drivers.

  • Key idea of testing programs with methods:

  • The “building block” approach.

  • Using methods, (1) one can build programs block by block and (2) one can test each block before going on to the next block.

  • See diagrams.


Stubs and drivers cont l.jpg

Stubs and drivers (cont.).

  • Why do it this way?

  • As much as possible we can eliminate errors before moving on to the next stage. Like a builder who wants to be sure the foundation is sound, then the walls, then the roof.

  • If an error is found at a later stage, it was probably caused by a later module.


Stubs and drivers cont7 l.jpg

Stubs and drivers (cont.).

  • But, how can we test individual methods?

  • Need to be able to run methods independently.

  • This uses the idea of “stubs and drivers.”

  • First create a driver = a skeleton Main program that can be used to call just the method(s) you want to test.


Stubs and drivers cont8 l.jpg

Stubs and drivers (cont.).

  • If a given method requires input from elsewhere (including input derived from another method), this can be simulated by sending dummy values to the method.

  • Then, one can code “stubs” for other methods. A stub=a method with an interface, but without a full implementation.


Stubs and drivers cont9 l.jpg

Stubs and drivers (cont.).

  • A stub may contain “a trace statement” e.g. “method X entered OK.” This can be used to test the logical structure of the program, to see if various pathways are followed correctly. For example one can check if the method is called if, and only if a certain condition is true.


Stubs and drivers cont10 l.jpg

Stubs and drivers (cont.).

  • One can also develop any of the stubs by filling in the implementation, one method at a time, and testing it.

  • Why? What is the advantage?


3 unit testing and integration testing l.jpg

3. Unit testing and integration testing.

  • Unit testing: testing each method separately as a method?

  • Why?

  • Analogy: checking that all of the parts for a model airplane / Bionicle are sound.

  • Unless each of the parts is sound, no chance that the whole will be.


Unit testing and integration testing cont l.jpg

Unit testing and integration testing (cont.).

  • But unit testing is not enough.

  • Why?

  • Just because each part is sound, does not mean that the whole will be (fallacy of composition). Why?

  • Compare: all players passed their fitness test / all of these are trained actors.


Unit testing and integration testing cont13 l.jpg

Unit testing and integration testing (cont.).

  • So, in addition to unit testing, need integration testing: checks that all of the modules (1) are correctly coordinated;

    (2) interface (communicate) correctly.

    Note that methods have pre-conditions (what should be true before the method is called) and postconditions.


Unit testing and integration testing cont14 l.jpg

Unit testing and integration testing (cont.).

  • The postconditions of an earlier method should be the preconditions of the next method. Analogy with soccer: one player needs to make a pass which “sets up” the next player for his play.

  • E.g. the preconditions of a calculation method are that all givens have been input and any values to calculate have been initialized (to zero or other initial value).


  • Login