DEV Community

ranms25
ranms25

Posted on

OSDC-2023-Assignment 11

Quick review of the lecture at the course OSDC

This week During this week's session on Testing with PyTest, we learned various concepts and techniques. We started the session with an introduction to testing using pytest. Covered topics such as test methods, demo tools in Python, an application under test (AUT), regression testing, doctest, and unit test. @szabgab also demonstrated how pytest can be used alongside unit test and doctest for comprehensive testing. Test coverage, pytest setup, and examples of tests were discussed as well.

Also, we focused on additional features and functionalities. Gabor explained pytest's handling of expected exceptions, changing text, missing exceptions, and raised exceptions. We also covered topics like exercise-testing exceptions, multiple failures, selective running of test functions, stopping on the first failure, and expecting a test to fail. The session included discussions on command-line options such as -rx, skip tests, show extra tests, and verbose and quiet modes.

Overall, the session provided a comprehensive overview of testing with PyTest and equipped us with various techniques and tools to write effective tests for Python applications.

Assignment

Find an open-source project, and write some tests for a part of the code that did not have tests. Send it as a Pull-Request.

Irish-C/calculator-class-program project


I created several test files to validate different aspects of the calculator program. These files are test_calculator.py, test_calculator_extended.py, test_gui.py, test_gui_extended.py, and test_user_validation.py.

test_calculator.py

This file contains tests to verify the basic functionality of the calculator class. The tests cover operations such as addition, subtraction, multiplication, and division. Here's an example test from the file:

def test_add():
    calculator = Calculator()
    assert calculator.add(2, 3) == 5
Enter fullscreen mode Exit fullscreen mode

test_calculator_extended.py

In this file, I expanded the tests to cover more complex scenarios. The tests include operations with negative numbers, decimal numbers, and large numbers to ensure accurate calculations.

test_gui.py

The test_gui.py file focuses on testing the graphical user interface (GUI) of the calculator. The tests verify if GUI elements are displayed correctly and if button clicks trigger the appropriate actions. Here's an example test:

def test_clear():
    gui = TkinterGUI()

    # Set the inputs
    gui.num1_entry.insert(0, "2")
    gui.num2_entry.insert(0, "3")
    gui.operation_var.set("Addition")

    # Call the clear method
    gui.clear()

    # Check if inputs and selection are cleared
    assert gui.operation_var.get() == "Select Operation"
    assert gui.num1_entry.get() == ""
    assert gui.num2_entry.get() == "" "
Enter fullscreen mode Exit fullscreen mode

test_user_validation.py

In this file, I wrote tests to validate user input. The tests ensure that the calculator class properly handles invalid inputs, such as non-numeric characters and division by zero. Here's an example test:

 def test_validate_user_addition(self):
     uv = UserValidation()
     result = uv.validate_user(2, 3, 'Addition')
     assert result == 5"
Enter fullscreen mode Exit fullscreen mode

To run the tests, use the pytest command in the PowerShell terminal while inside the "calculator-class-program" directory. pytest will collect the test items from the files and execute them, displaying the progress and results.

During the test execution, all tests passed successfully, indicating that the relevant parts of the calculator program are functioning correctly.

Overall, the test files were created to validate different aspects of the calculator program, including basic calculations, extended operations, GUI functionality, and user input validation. The tests help ensure the accuracy and robustness of the calculator application.

Calculator-Class-Program GitHub Action

This GitHub Action enables the automation of the testing process for the Irish-C/calculator-class-program project. The action is triggered whenever there is a push or pull request event in the repository. It sets up the necessary environment, installs dependencies, and runs the test suite using the PyTest framework.

Test Results

The GitHub Action provides detailed test results, indicating the pass or fail status of each test case. These results can be reviewed in the GitHub Actions tab of the repository. Any failures or errors will be displayed, helping identify any issues in the calculator-class-program code.

By leveraging this GitHub Action, the Irish-C/calculator-class-program project benefits from automated testing, ensuring the reliability and accuracy of the calculator program. Developers can easily identify and address any issues that arise, promoting code quality and maintainability.

Keep following along as I continue to learn and make progress week after week!


Top comments (0)