Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Add Simple Tests | Sustaining Code Quality
Code Quality and Refactoring in Python

bookChallenge: Add Simple Tests

In this exercise, you will practice writing basic tests to check the correctness of a Python function. Testing is essential for maintaining code quality because it helps you verify that your code works as expected and catches mistakes early. You will be given a function, and your goal is to write a few assert-based tests that confirm the function behaves correctly for different inputs.

Oppgave

Swipe to start coding

Write assert-based tests to verify the reverse_string function works as intended.

  • Write at least three assert statements that check the output of reverse_string for different input strings.
  • Use the following template for your assert messages: "Expected {expected} but got {actual}".
  • Place your assert statements after the function definition.

Løsning

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 5
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

What is the Python function I need to write tests for?

Can you give me an example of how to write an assert-based test?

What are some common mistakes to watch out for when writing tests?

close

Awesome!

Completion rate improved to 5.26

bookChallenge: Add Simple Tests

Sveip for å vise menyen

In this exercise, you will practice writing basic tests to check the correctness of a Python function. Testing is essential for maintaining code quality because it helps you verify that your code works as expected and catches mistakes early. You will be given a function, and your goal is to write a few assert-based tests that confirm the function behaves correctly for different inputs.

Oppgave

Swipe to start coding

Write assert-based tests to verify the reverse_string function works as intended.

  • Write at least three assert statements that check the output of reverse_string for different input strings.
  • Use the following template for your assert messages: "Expected {expected} but got {actual}".
  • Place your assert statements after the function definition.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 5
single

single

some-alt