views:

37

answers:

2

I have a python program that imports pythoncom (and uses pythoncom.CoCreateInstance from it). I want to create a unittest for the program logic without it importing pythoncom (so I can run the test on Linux as well).

What options are there? Can I do it without modifying the system under test?

What I found so far:

sys.modules["pythoncom"] = "test"
import module_that_imports_pythoncom

My problem with it is if I have:

from pythoncom.something import something

I'll get:

ImportError: No module named something.something

And sys.modules["something.something"] or sys.modules["pythoncom.something.something"] doesn't work.

Any ideas?

A: 

You could put import pythoncom into a try except block.

Skilldrick
I said I don't want to modify the system under test, it should not work if it can't find pythoncom. What I want is in my tests to ignore or mock the pythoncom import so I can mock some functions from it.
vaidab
A: 

Ok, what if you modify PYTHONPATH in the tests, and make a new package on the filesystem in a testing directory called pythoncom with the necessary subdirectories?

Skilldrick
This would work but I'm looking on a simple solution, can I mark this module as already imported from my tests? Or replace it with a mock? I don't want to be os dependant when running the tests.
vaidab