Unit testing Flashcards

(19 cards)

1
Q

what is a unit test

A

A unit test is a test that checks the behavior of a single logical unit (usually a class or function) in isolation.

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

What is continuous integration

A

Developers work on local machines and merge working code frequently into a shared branch.

On each merge, automatic builds and tests run.

If tests pass → the merge is successful

If tests fail → the merge is blocked

Goal: catch integration issues early and keep the main branch stable.

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

What is continuous delivery

A

After CI:

The application is packaged

More tests run (integration, acceptance, performance, etc.)

The system is always in a deployable state

Deployment is manual (someone chooses when to release).

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

continuous deployment

A

Continuous deployment is:

CI + CD + automatic deployment

If all tests pass, the application is automatically deployed to production, with no manual approval.

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

why unit testing

A
  1. Improves Design: Leads to modular and testable code.
  2. Supports Refactoring: Tests act as safety nets during code changes.
  3. Confidence Boost: Helps developers experiment without fear of breaking existing functionality.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

smoke test

A

a series of core tests that ensures the basic functionality of system is working

doesnt guarantee that theres no bugs just there are no fatal ones so we can do deeper debugging

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

Critical Factors for Automated Tests

A

Repeatability & Consistency: Tests should give consistent results every time they run

Readability & Maintainability: Tests act as documentation; keep them simple and understandable.

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

Testability 2 features

A
  • Visability - the tester can see and understand what happens within the prod system and can observe important aspects
    of the internal state of the system
  • Control - the tester can force interesting things to happen within the prod system i.e. control its behaviour

Low control → real database, real network, real services

High control → mocks, stubs, fake databases, test doubles

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

unit test requirements

A
  • Easy to write a test
  • Easy to find a test
  • Easy to test different **aspects of a contract
  • Easy to maintain tests
  • Easy to run tests
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

3 A’s of unit testing

A

Arrange - Set up test data and unit under test

act - call the method under test

assert - verify result matches expectation

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

general rules for unit tests:

A
  • Create one test class per application class.
  • Focus on one scenario per test method.
  • Tests should:
    • Be named after the method they test.
    • Be placed in a parallel directory structure.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Agile test quadrant watch the video for him to explain this this is hard to question but its in the sheet

A

zcxz

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

Advantages of test automation

A
  1. Repeatability and Consistency:
    • Tests can run multiple times with consistent results.
    • Ensures the same tests are executed in every iteration.
  2. Faster Feedback:
    • Reduces time between code changes and test results.
    • Speeds up the identification of defects.
  3. Improves Quality:
    • By running tests frequently, automation helps identify bugs earlier in the development cycle.
  4. Support for Regression Testing:
    • Automated tests are invaluable for ensuring new code changes don’t break existing functionality.
  5. Reduces Manual Effort:
    • Tedious tasks are automated, freeing testers to focus on exploratory and edge case testing.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Critical Success Factors for Test Automation

A
  1. Maintainability:
    • Automated tests should be easy to update as the application evolves.~
  2. Readability:
    • Test scripts should be clear and act as documentation.
  3. Robustness:
    • Tests should handle minor changes in the system without failing unnecessarily.
  4. Focus on Key Areas:
    • Automate tests for critical paths and frequently used features.
  5. Repeatability and Consistency
    1. when a test is complete, it should pass repeatedly; wheter it executes by itself or within a test suite
    2. when a test fails - we need to pinpoint the cause - did the test uncover a bug or is the test itself faulty
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Smart

A

Self contained and stateless
- should not rely on global state
- tests should be independent and be able to be run in any order

Maintainable
- if you change development code you should easily be able to update tests

Act as documentation
-test name should easily tell us what we are testing

Robust
- minor changes should not cause test to fail

To the point
- test should focus on one thing
- when fails easy to pinpoint error

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

test driven development

A

Test-Driven Development (TDD)

Write a test for a functionality

The test should fail (red)

Write just enough code to make the test pass

No extra logic (green)

Refactor the code

Improve code quality while keeping all tests passing

Repeat

Add new tests until no further behaviour is required

17
Q

example of tdd

A

TDD Example: isEven(n)

Write a failing test
Test: isEven(2) should return true.
The test fails because the function does not exist.

Write minimal code to pass the test
isEven(n):
return true
The test passes, but the implementation is clearly incorrect.

Add another test
Test: isEven(3) should return false.
This test now fails.

Improve the implementation
isEven(n):
return n % 2 == 0
Both tests now pass.

Refactor (if necessary)
The code is already simple and clear, so no further refactoring is needed.

18
Q

benefits of tdd

A
  1. Benefits:
    • Forces intentional design of interfaces.
    • Encourages high cohesion and low coupling.
    • Provides automated regression tests.
19
Q

disadvantages of tdd

A
  1. Challenges:
    • Buggy tests can mislead development.
    • High test maintenance cost if not written carefully.