0
@pytest.mark.usefixtures("oneTimeSetUp","setUp")
class BeyondTest(unittest.TestCase):
    log = cl.testLogger(logging.INFO)
    @pytest.fixture(autouse=True)
    def classSetup(self,oneTimeSetUp):
        self.ts = TestStatus(self.driver)
        self.bmf = BMF(self.driver)


    @pytest.mark.run(order=1)
    def test_site_to_csv(self):
        self.bmf.imagelist()
        first_column =[l[4] for l in self.bmf.csvreader]
        list_site_to_csv = [item for item in self.bmf.full_list if item not in first_column]
        self.log.INFO(list_site_to_csv)
        assert len(list_site_to_csv)<=0

So I have a csv with some data that needs to be verified on a site, I first capture data on the site append to a list "self.bmf.full_list", the csv columns are saved in the first_column list, I then compare both list and save the ones missing from csv but are present on site to the list "list_site_to_csv. The assertion is the len(list_site_to_csv)<=0. What I am trying to figure out is how to log.INFO only when an assertion fails? something like

if assert len(list_site_to_csv)<=0:
    then self.log.INFO(list_site_to_csv)

anyother suggestion on better way to handle this than a list would also be great

1
  • And you don't just want an if-statement? You could put the assertion in a try/except block to catch the exception, log, then raise the exception. Commented Mar 31, 2020 at 2:55

1 Answer 1

2

assert accepts a second argument. Example:

>>> list_site_to_csv = ['apples']
>>> assert not list_site_to_csv, f'It failed. List is not empty. {list_site_to_csv}'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: It failed. List is not empty. ['apples']

In this case if the list_site_to_csv is not empty, the assert will pops out, and it'll show the message.

Note:

len(list_site_to_csv) <= 0 is not pythonic. For testing falsy values it's preferred to use if not my_var:.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.