Skip to main content

A unit testing framework with colorful messages.

Project description

Colorful Test

Colorful Test is a unit testing framework that is similar to and inspired by Python's unittest. What makes Colorful Test cool and different is that it's uniquely designed for educational settings while also supporting general-purpose use. It displays error messages in a colorful and more helpful way, and success messages in a way that makes students proud of their work.

It also guides students through their projects by failing fast (by default) and showing them exactly what went wrong. unittest doesn't give you much freedom when it comes to the order of test execution, but this is so important for student projects that, with Colorful Test, you can just append a number at the end of the test name to determine the order.

We also provide a grade/score feature (optional) that makes it easy to grade student projects. The run method returns a TestResults object that lets you customize how the output looks, giving you more control.

If you're familiar with unittest, it shouldn't be hard to get a grasp of how Colorful Test works, and you can skip to the official docs.

Installation

To use Colorful Test, first install it using pip:

$ pip install colorful-test

Documentation

Read the full documentation here.

Basic Example

Colorful Test provides a handful of useful tools to help you construct and run tests. Here's a basic example of some of those tools in action:

from colorful_test import TestCase, show_message
from solution import add, mul, div

class TestSolution(TestCase):

    @show_message(
        success='Your add method works as expected',
        fail='''
        Your add method doesn't work as expected. Hints:

        Expected: %s
        Received: %f
        '''
    )
    def test_basic_addition_2(self):
        self.assert_equal(add(1, 1), 2)
        self.assert_equal(add(1, 2), 3)

    @show_message(
        success='Your mul method works as expected',
        fail='''
        Your mul method doesn't work as expected. Hints:

        Expected: %s
        Received: %f
        '''
    )
    def test_basic_multiplication_0(self):
        self.assert_equal(mul(1, 1), 1)
        self.assert_equal(mul(1, 2), 2)

    @show_message(
        success='Your div method works as expected',
        fail='Your div method should raise a ZeroDivisionError if the second argument is 0'
    )
    def test_basic_division_error_1(self):
        self.assert_raises(ZeroDivisionError, div, 3, 0)

if __name__ == '__main__':
    TestSolution.run_and_output_results()

A TestCase is created by inheriting TestCase. The test runner looks for methods that start with test_ and considers them as tests. These tests are then ordered based on the number appended to their names.

In each test, assert_equal or assert_raises can be used—these work similarly to assertEqual and assertRaises from the unittest framework.

  • assert_equal checks if the first and second arguments are equivalent.
  • assert_raises verifies whether the specified callable raises an error with the given arguments.
  • Alternatively, you can simply use assert, and the test runner will still accumulate all test results and generate a test report.

One thing to note is that test_basic_multiplication_0 will be executed first, followed by test_basic_division_error_1, and so on. This happens because of how the tests are ordered.

Appending a number to test method names is not mandatory—if omitted, test methods will be ordered alphabetically.

Tutorial

This tutorial will guide you on how to write unit tests using Colorful Test.

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork this repository.

  2. Clone the repository to your own machine by running one of the following commands:

    • HTTPS
    git clone https://github.com/<your-username>/Colorful-Test.git
    

    OR

    • SSH
    git clone git@github.com:<your-username>/Colorful-Test.git
    

    OR

    • Github CLI:
    gh repo clone <your-username>/Colorful-Test
    
  3. Create a new branch. The name of the branch must reflect the change you are about to make.

    git checkout -b <branch-name>
    
  4. Make your changes or add your new feature. Remember to commit early and commit often. Read our commit rules here.

    • Short commit messages:
      git add <changed-files>
      git commit -m "<commit-message>"
      
    • Long commit messages:
      git add <changed-files>
      git commit
      
  5. Push your changes to Github by running this command:

    git push origin <branch-name>
    
  6. Go over to GitHub and create a pull request. Make sure to include a comment explaining the additions. Please include the issue number being addressed in your comment. For instance, if you were resolving issue 6, add Issue: #6 at the end of your comment. For more information, please refer to our contributing rules here.

More ways to contribute

You can contribute not only by writing code but also by assisting us in enhancing this README, drafting documentation, creating tutorials, and more.

  • Fix spelling and grammatical errors across our documentation.
  • Enhance this README and other docs.
  • Create more tutorials on our website.
  • Produce video tutorials.
  • Write tests.
  • Design a logo for us.
  • Report bugs.
  • Suggest additional features.

Licence

Distributed under the Apache License 2.0. See LICENSE for more information.

Contact

Email - nqabenhlemlaba22@gmail.com

Instagram - instagram.com/asanda.que

GitHub - github.com/lindelwa122

LinkedIn - linkedin/nqabenhle

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

colorful_test-1.0.1.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

colorful_test-1.0.1-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file colorful_test-1.0.1.tar.gz.

File metadata

  • Download URL: colorful_test-1.0.1.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.10.12 Linux/6.9.3-76060903-generic

File hashes

Hashes for colorful_test-1.0.1.tar.gz
Algorithm Hash digest
SHA256 7e82a61fd6095acff3166b411873b8657b98bf8157c8f9bd4647f3983b4447ca
MD5 960c782dce5fc1fd05d9fe233129e727
BLAKE2b-256 51b0102a23a9614447c8e9d256c55945407cc20bdbb0b50e9259f62c47e10edf

See more details on using hashes here.

File details

Details for the file colorful_test-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: colorful_test-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.10.12 Linux/6.9.3-76060903-generic

File hashes

Hashes for colorful_test-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 36d7bb3c184140fe8e18932c5a3556c622a2a6fda4f593787dfa81cbef49f8e3
MD5 289166cb3106ea52e9cdd91a50626d13
BLAKE2b-256 32fbb9b13e1b7faa1eb0149b9c408637d9533149d2b49ed35b3f95c73ea8135c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page