Home > OS >  Pytest - 2 pytest tests, one should pass one should fail
Pytest - 2 pytest tests, one should pass one should fail

Time:07-03

I have an assignment which asks to do a pytest test and :

· Test should PASS if the string “ Right-click in the box below to see one called 'the-internet' “ found on page.

· Another Test should FAIL if string “Alibaba” is not found on the same page.

my code :

import pytest

def test_text1():
    text1 = "Right-click in the box below to see one called 'the-internet'"
    if text1 in driver.page_source:
        assert text1 in driver.page_source

def test_text2():
    text2 = "Alibaba"
    if text2 in driver.page_source:
        assert text2 in driver.page_source

I'm trying to write a python script and do the pytest test with the Pycharm terminal, and the two tests pass, although the first one should pass and the second should fail. Do you have any idea how can I solve this? Thanks!

CodePudding user response:

There is no need for an if statement in an assert:

import pytest

def test_text1():
    text1 = "Right-click in the box below to see one called 'the-internet'"
    assert text1 in driver.page_source

def test_text2():
    text2 = "Alibaba"
    assert text2 in driver.page_source

You can improve that further by using parametrize:

@pytest.mark.parametrize("text",
["Right-click in the box below to see one called 'the-internet'",
"Alibaba"]
)
test_contains_text(text, driver):
    assert text in driver.page_source
  • Related