Image assets won't load when using pytest
Asked Answered
Y

1

6

I'm developing a GUI with PySide and I create pixmap from images like this:

PHONE_IMAGE_PATH = "resources/images/phone.png"
self.phone_null = QtGui.QPixmap(GREY_DOT_PATH)
self.phone_null = self.phone_null.scaledToWidth(20)

This works perfectly when I run the program but when I test it with py.test this happens:

QPixmap::scaleWidth: Pixmap is a null pixmap

It says that the pixmap is null, so it seems like the image wasn't loaded properly.

I really want to test my code and I can't find any information about this problem anywhere. Does anyone have a solution to my problem?

Yellowknife answered 14/4, 2016 at 10:40 Comment(1)
Use an absolute path.Fencer
C
1

From Wikipedia:

By contrast, a relative path starts from some given working directory, avoiding the need to provide the full absolute path. A filename can be considered as a relative path based at the current working directory. If the working directory is not the file's parent directory, a file not found error will result if the file is addressed by its name.

It seems that ou are using a relative path for your image asset, so when you run your program the relative path is correct, but i assume that your tests are located in different directory from your main program.

You could obtain current file location with: __file__
Example:

main_script_dir = os.path.dirname(__file__)
rel_path = "resources/images/phone.png"
PHONE_IMAGE_PATH = os.path.join(main_script_dir, rel_path)
Chastain answered 19/9, 2016 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.