Including Pytest-check

In our series we have created a very basic but solid framework with all the good practices in POM.

To round up our tests, In JAVA, we have something called soft-assert, which basically is a way to validate multiple conditions without the execution being stopped after the first failure. We have the following in our current implementation:

assert result_page.link_div_count() > 0
assert result_page.phrase_result_count(PHRASE) > 0
assert result_page.search_input_value() == PHRASE

The problem here is that we are assuming that everything will work as expected. However, if the first assertion fails, then the execution will stop and the other conditions won't be executed. Which to some extends that is OK because we quickly will see that something is not broken, but what if we wanted to know if the other assertions have problems as well? That's there Pytest-check comes into place. It is our powerful library to apply soft-asserts and besides having all the regular methods we would want to use in our assertions, you can also build your own methods (which will be not addressed here).

To make use of this powerful library, proceed as follows.

  1. Install pytest-check pipenv install pytest-check --dev
  2. Import the library import pytest_check as check
  3. Update your assertions to use check instead of assert
  4. Run

In the end, your test should look like below:

import pytest

from pages.search import DuckDuckGoSearchPage
from tests.base_test import BaseTest

import pytest_check as check

class TestBasicSearch(BaseTest):

    def test_basic_duckduckgo_search(self):
        # Set up test case data
        PHRASE = "panda"

        # Search for the phrase
        search_page = DuckDuckGoSearchPage(self.driver)
        result_page = search_page.search(PHRASE)

        # Verify that results appear   
        check.greater(result_page.link_div_count(), 0)
        check.greater(result_page.phrase_result_count(PHRASE), 0)
        check.equal(result_page.search_input_value(), PHRASE)

And that's it, pretty simple setup for a powerful library that will take your project to the next level!