Monday, October 10, 2022

How to install pytest, naming convention and running tests individually,in a folder, filter with filename, filter with testcase name, filter base on a group

 to install pytest:

pip install pytest


naming convention:

file name and testcase/method name: test_***.py


arguments:

-k method names execution

-s logs in output

-v for more info


sample testcase file with multiple test cases with above naming convention followed.


test_demo2.py

---

def test_firstProgram():  ## method name or test case name should start with test_***
  msg = "Hello"
  assert msg = "Hi", "Test failed because strings do not match"
def test_SecondProgram():
  a = 4
  b = 6
  assert a+2 == 6, "Addition do not match"
def test_SecondPCreditCard():
  a = 4
  b = 6
  assert a+2 == 6, "Addition do not match"

To run all python tests in a folder, py.test -v -s
to run a particualr test file,  py.test test_demo2.py -v -s
to filter based on test case names, py.test -k CreditCard -v -s

we can group test cases based on annotations.


@pytest.mark.smoke
def test_firstProgram():
  msg = "Hello"

### in another file:
@pytest.mark.smoke
def test_thirdProgram():
  msg = "Hello morning"
py.test -m smoke -v -s

to skip few tests and run remaining.

@pytest.mark.smoke
@pytest.mark.skip
def test_firstProgram():
  msg = "Hello"

## this particular test will be skipped.
## py.test -v -s
if  we want to make a test to run but skip the reporting of that, so the test will run and the errors won't stop remaining tests.


@pytest.mark.xfail
def test_firstProgram():
  msg = "Hello"

No comments:

Post a Comment