Week 5 Flashcards

1
Q

What is Unit Testing?

A

Testing individual parts (units) of code to ensure they work correctly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the purpose of Unit Testing?

A

To test that the code works as intended

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Remember…

A

It is often very difficult or impossible to “prove” a piece of code works, we can only satisfy ourselves that we havent disproved it works

Designing our tests while keeping this in mind becomes critical

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the importance of Unit Tests?

A

Reduces time spent debugging

Helps communicate code’s intended use - illustrates how you expect the code to perform on various inputs and conditions

Code shouldn’t drift away from the limits put on them by tests (unless you stop running them)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you prepare a Unit Test?

A

First decide how to test that aspect of the code in question - typically a unit test will test a single method, and some subset of the funtionality that method may provide

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the Right-BICEP pneumonic mean?

A

Right - Are the boundary results correct (or “right)

B - Are all the Boundary conditions correct?

I - Can you check inverse relationships?

C - Can you cross-check the results using some other means?

E - Can you force error conditions to occur?

P - are the performance characteristics within bounds?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the properties of good tests (using pnemonic)?

A

You’ve flexed your Right-BICEP

Automatic

Thorough

Repeatable - (should produce same results each time)

Independant - (of each other test and of the environment)

Professional - (written and maintained to same standard as the shipped code)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How should Unit Tests be structured?

A

If production code class is called Account and a method is called createAccount

The test should be called testCreateAccount

Method testCreateAccount will call createAccount with necessary arguments to ensure createAccount operates as expected

You may have more than one test method that tests createAccount but they should all begin with test

eg. testCreateAccountDuplicates

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an Assertion?

A

JUnit frameworks have assertions which are fundamental building blocks of a unit test

A single unit test may have one to many assertions within it - althought a unit test is concerned with a specific subset of the functionality of an object

Assertions help verify that the code behaves as expected during unit tests.

Example: If you have a function that adds two numbers, you might assert that the result is correct:

assertEquals(5, add(2, 3));

This checks if adding 2 and 3 gives you 5.
If True: The test passes, meaning your code works as intended.

If False: The test fails, signaling that there’s a problem to fix

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the AssertEquals method?

A

assertEquals (String msg, expected, actual)

String optional and will be displayed if test fails

expected - value you would hope to see

actual - value produced in code under test

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why do you have to be careful with AssertEquals and floating points?

A

Due to representation limitations (finite memory) floating point values are not exact - checking exact quality of floating point values is therefore unwise

Instead - specify some tolerance of nearness

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are some more examples of Assertions?

A

assertNull (String msg, java.lang.Object object)

AssertNotNull (String msg, java.lang.Object object)

assertSame (String msg, expect, actual) - Checks object references are the same

assertNotSame (String msg, expected, actual)

assertTrue (String msg, boolean condition)

assertFalse (String msg, boolean condition)

fail (String msg) - will fail test immediately if flow of control passes to this statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are Mock Objects?

A

Can be difficult when object depends on other objects/ parts of system which are hard to control or reason about

Cheap placeholder objects that are predictable and allow us to test the method we need to in the current class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

In what cases should you use Mock Objects?

A

When the real object:

  • has non-deterministic behaviour
  • is difficult to set up
  • has behaviour that is hard to trigger (eg. network error)
  • is slow
  • has (or is) a user interface

*does not yet exist

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the benefits of Test-Driven Development?

A

Can help you design code better

You can see the code from the user’s perspective - not the developers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are Invariants?

A

Assertions about classes/ objects that should not change - should definitely test these when developing unit tests

eg.

In a class implementing an array - an array index >=0, and < array length

In a bank account, credits and debits match the balance

Measurements - amounts measured in different units should match after conversion

Need to ensure an object can never be viewed in an inconsistent state

17
Q
A