Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #15 from jitseniesen/fix-test
Browse files Browse the repository at this point in the history
Use sys.getsizeof() to get expected memory consumption in tests
  • Loading branch information
jitseniesen authored Sep 19, 2017
2 parents 0415969 + 4da36a2 commit a719e90
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions spyder_memory_profiler/widgets/tests/test_memoryprofiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

"""Tests for memoryprofiler.py."""

from __future__ import division

# Standard library imports
import os
import sys

# Third party imports
from pytestqt import qtbot
Expand Down Expand Up @@ -35,7 +38,12 @@ def foo():
foo()"""

def test_profile_and_display_results(qtbot, tmpdir, monkeypatch):
"""Run profiler on simple script and check that results are okay."""
"""
Run profiler on simple script and check that results are okay.
This is a fairly simple integration test which checks that the plugin works
on a basic level.
"""
os.chdir(tmpdir.strpath)
testfilename = tmpdir.join('test_foo.py').strpath

Expand All @@ -61,12 +69,20 @@ def test_profile_and_display_results(qtbot, tmpdir, monkeypatch):
for i in range(6):
assert top.child(i).data(0, Qt.DisplayRole) == i + 1 # line no

# column 2 has increment (in Mib); displayed as 'xxx MiB' so need to strip
# last 4 characters
assert float(top.child(2).data(2, Qt.DisplayRole)[:-4]) >= 7 # increment (MiB)
assert float(top.child(2).data(2, Qt.DisplayRole)[:-4]) <= 8
assert float(top.child(3).data(2, Qt.DisplayRole)[:-4]) >= 150
assert float(top.child(3).data(2, Qt.DisplayRole)[:-4]) <= 160
assert float(top.child(4).data(2, Qt.DisplayRole)[:-4]) >= -160
assert float(top.child(4).data(2, Qt.DisplayRole)[:-4]) <= -150
# Column 2 has increment (in MiB); displayed as 'xxx MiB' so need to strip
# last 4 characters. Allow for 5% margin in measured memory consumption.
measured = float(top.child(2).data(2, Qt.DisplayRole)[:-4])
list_size_in_mib = sys.getsizeof([1] * 10 ** 6) / 2 ** 20
assert measured >= 0.95 * list_size_in_mib
assert measured <= 1.05 * list_size_in_mib

measured = float(top.child(3).data(2, Qt.DisplayRole)[:-4])
list_size_in_mib = sys.getsizeof([2] * 2 * 10 ** 7) / 2 ** 20
assert measured >= 0.95 * list_size_in_mib
assert measured <= 1.05 * list_size_in_mib

measured = float(top.child(4).data(2, Qt.DisplayRole)[:-4])
assert measured >= -1.05 * list_size_in_mib
assert measured <= -0.95 * list_size_in_mib

assert float(top.child(5).data(2, Qt.DisplayRole)[:-4]) == 0

0 comments on commit a719e90

Please # to comment.