MulaiMulai sekarang secara gratis

Factorial with unittest

In this exercise, you will start using unittest to create basic tests for the factorial function. Since the library uses an object-oriented approach, the functions will be implemented as methods of a unittest.TestCase class. The unittest package has already been imported.

Latihan ini adalah bagian dari kursus

Introduction to Testing in Python

Lihat Kursus

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

def func_factorial(number):
    if number < 0:
        raise ValueError('Factorial is not defined for negative values')
    factorial = 1
    while number > 1:
        factorial = factorial * number
        number = number - 1
    return factorial

class TestFactorial(unittest.TestCase):
    def test_positives(self):
        # Add the test for testing positives here
        ____
Edit dan Jalankan Kode