teddecor.UnitTest

Unit Testing

This modules includes, a test runner, test suites, test classes, test cases(functions), asserts, mocks, and much more.

Below are a few generic levels of testing which can be mixed together however you want.

  1. Use the provided runTest function to run a single method with the @test decorator

    from teddecor.UnitTest import *
    
    @test
    def test_case():
        assert True
    
    if __name__ == "__main__":
        runTest(test_case)
    
  2. Use the provided Test class to group test cases (functions) with the @test decorator.

    • This meant to group test that are focuses on a specific functionality
    from teddecor.UnitTest import *
    
    class TestClass(Test):
        @test
        def test_case(self):
            assert True
    
    if __name__ == "__main__":
        TestClass().run()
    
  3. Use the provided TestSuite class to group test classes and test cases together

    • This is meant to group a more general idea. Like the tests for a app can be split into backend and front end. Where both backend and frontend are their own test suite. Then inside the suites you get more focuses.
    from teddecor.UnitTest import *
    
    class BackendInput(Test):
        @test
        def user_input(self):
            assert True
    
        @test
        def file_input(self):
            assert True
    
    @test
    def app_response():
        raise NotImplementedError
    
    if __name__ == "__main__":
        TestSuite(
            name="Backend"
            tests=[
                BackendInput,
                app_response,
            ]
        ).run()
    
    • As you can see in the above example, you can use raise NotImplementedError to skip a test which also gives the message of Not Implemented
  4. Use provided Asserts and AssertThat modules to add readable, easy to use, testing assertions.

    from teddecor.UnitTest import *
    
    @test
    def assert_that():
        assertThat(12, eq(12))
        # Reads as, assert that 12 is equal to 12
        assertThat("the", within(["the", "moon"]))
        # Reads as, assert that "the" is within ["the", "moon"]
        assertThat("the moon", has("moon"))
        # Reads as, assert that "the moon" has "moon"
    
    @test
    def base_asserts():
        Asserts.assert_equals(12, 12)
    
    • Read the documentation for the Asserts and AssertThat modules for more information along with how you can create your own functions for assertThat

Below is an example test (example.py)

from teddecor.UnitTest import *

class Example(Test):
  @test
  def test_pass(self):
    assert True

if __name__ == "__main__":
  Example().main()

The above example shows that you need to have a class that inherits from UnitTest.Test. Then when you run the main() function from an instance of that class, you get the results printed out. The main() function will run any method in the class that has the @test decorator.

Look at the repositories example/UnitTest/overview.py file to see more detailed examples

  1"""Unit Testing
  2
  3This modules includes, a test runner, test suites,
  4test classes, test cases(functions), asserts, mocks, and much more.
  5
  6Below are a few generic levels of testing which can be mixed together however you want.
  7
  81. Use the provided `runTest` function to run a single method with the `@test` decorator
  9    ```python
 10    from teddecor.UnitTest import *
 11    
 12    @test
 13    def test_case():
 14        assert True
 15
 16    if __name__ == "__main__":
 17        runTest(test_case)
 18    ```
 19
 202. Use the provided `Test` class to group test cases (functions) with the `@test` decorator.
 21   * This meant to group test that are focuses on a specific functionality
 22    ```python
 23    from teddecor.UnitTest import *
 24    
 25    class TestClass(Test):
 26        @test
 27        def test_case(self):
 28            assert True
 29
 30    if __name__ == "__main__":
 31        TestClass().run()
 32    ```
 33
 343. Use the provided `TestSuite` class to group test classes and test cases together
 35   * This is meant to group a more general idea. Like the tests for a app can be split into backend and front end. Where both backend and frontend are their own test suite. Then inside the suites you get more focuses.
 36    ```python
 37    from teddecor.UnitTest import *
 38    
 39    class BackendInput(Test):
 40        @test
 41        def user_input(self):
 42            assert True
 43        
 44        @test
 45        def file_input(self):
 46            assert True
 47
 48    @test
 49    def app_response():
 50        raise NotImplementedError
 51
 52    if __name__ == "__main__":
 53        TestSuite(
 54            name="Backend"
 55            tests=[
 56                BackendInput,
 57                app_response,
 58            ]
 59        ).run()
 60    ```
 61
 62   *  As you can see in the above example, you can use `raise NotImplementedError` to skip a test which also gives the message of `Not Implemented`
 63
 644. Use provided `Asserts` and `AssertThat` modules to add readable, easy to use, testing assertions.
 65   ```python
 66    from teddecor.UnitTest import *
 67
 68    @test
 69    def assert_that():
 70        assertThat(12, eq(12))
 71        # Reads as, assert that 12 is equal to 12
 72        assertThat("the", within(["the", "moon"]))
 73        # Reads as, assert that "the" is within ["the", "moon"]
 74        assertThat("the moon", has("moon"))
 75        # Reads as, assert that "the moon" has "moon"
 76
 77    @test
 78    def base_asserts():
 79        Asserts.assert_equals(12, 12)
 80   ```
 81   * Read the documentation for the `Asserts` and `AssertThat` modules for more information along with how you can create your own functions for assertThat
 82   
 83Below is an example test (`example.py`)
 84
 85```python
 86from teddecor.UnitTest import *
 87
 88class Example(Test):
 89  @test
 90  def test_pass(self):
 91    assert True
 92
 93if __name__ == "__main__":
 94  Example().main()
 95```
 96
 97The above example shows that you need to have a class that inherits from `UnitTest.Test`. Then when you run the `main()` function from an instance of that class,
 98you get the results printed out. The `main()` function will run any method in the class that has the `@test` decorator.
 99
100Look at the repositories [example/UnitTest/overview.py](https://github.com/Tired-Fox/TEDDecor/tree/main/examples/UnitTest/overview.py) file to see more detailed examples
101"""
102
103from .Asserts import *
104from .Testing import *
105from .TestSuite import *
106from .Results import *
107from .Objects import *