1

I am using a (as far as I can tell) Raspberry Pi specific library (smbus) for my project. I would like to use pytest to test the software both on my machine and with gitlab-ci on my school's server. Not that it matters, but this is for a club project not homework.

A description of my environment:

I am using Ubuntu 17.04/PyCharm for my development environment. The gitlab runner that I have access to is docker based (I choose the environment).

Neither of the above two have the ability for smbus or the ability for i2c comms.

I am wondering if it is possible to tell pytest to ignore the smbus import without adding a try/except around the import?

Thanks.

Tyler Fricks
  • 91
  • 1
  • 8

1 Answers1

1

If you really want to avoid a try/accept you could have a function return the import...

def get_smbus():
    import smbus
    return smbus

smbus = get_smbus()

This gives you the ability to use the mock library to overwrite the get_smbus function with ease.

But I personally would use try / except and catch ImportError.

Shadow
  • 8,749
  • 4
  • 47
  • 57
  • Thanks. I just assumed that the try/except was bad practice. – Tyler Fricks Oct 02 '17 at 19:45
  • try except is actually a really good practice - as long as you catch specific exceptions. Catching all errors and ignoring them is the bad practice. – Shadow Oct 02 '17 at 22:03