A framework (based on python) that makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.

Why do we need Pytest ? why selenium is not enough ?

Selenium performs an action in the browser.

But how we'll create a test to assert our case ? How we’ll run test suite (a bunch of test cases) ?

That's right... we need PyTest...

  • Build our code structure (Fixtures)

  • Create Test Suites

  • Test Runners

  • Provide Assertions


Let's not waste time and start coding…

  • Create Folder My_tests

  • Create python file test_utils

1def twice(x): 2 return x * 2 3 4 5def thrice(x): 6 return x * 3

 

Code conventions:

Files start (or end) with: test_

Methods start with: test_

Class starts with: Test_

Install PyTest:

Install:

pip install -U pytest

Verify correct version:

pytest --version












How to tun PyTest ?

Note: When you run pytest, by default, it will look for tests in all directories and files below the current directory.

We would like to execute this method: test_demo




Step 1:



Step 2:

Click on Templates → Python tests → pytest




Another option is to run pytest from a CLI:

Type pytest will run all the files under this folder (with the pytest convention)




Type pytest -v -s v= verbose, s=print out on terminal (which mean if I have some print function, print it)




Type pytest -v -s <file_name> will run pytest only on the specific file




If I want to run pytest only on a specific function, we can run: pytest -v -s -k example




Result:

    
        

If to be more pragmatic, I have some files I can create a sanity function def test_sanity(): and when we’ll want to run only sanity, it will be very easy !

If I want to skip on some function ? it’s easy ! just give him above @pytest.mark.skip





Result:



Why I'll want to skip a function ? maybe, for example, I know that there is a temporary bug, instead to delete this function I can just skip on him...

Fixtures

  • Fixture can be thought of as metadata for classes.

  • These are reserved words.

  • Their purpose is to structure the code conveniently.







@classmethod



Assertions:

  • Assertions are checks that return True or False status.

  • In pytest, if an assertion fails in a test method, then that method execution is stopped there

  • The remaining code on the test method is not executed and pytest will continue with the next test method.


Comments

Popular posts from this blog

Selenium - locators

Selenium Webdriver Manager

Selenium - brief

אבטחת מידע

Page Object Model, what is it ?

תבניות עיצוב - מפעל

Security Informtion