Tyler Fricks
Tyler Fricks

Reputation: 111

Can pytest ignore import dependencies? I want to unit test Pi code

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.

Upvotes: 1

Views: 1531

Answers (1)

Shadow
Shadow

Reputation: 9427

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.

Upvotes: 1

Related Questions