Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test_time being always 0.0 #292

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion green/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ def startTest(self, test: RunnableTestT) -> None:
Called before each test runs.
"""
test = proto_test(test)
self.start_time = time.time()
self.reinitialize()
self.start_time = time.time()
if self.start_callback:
self.start_callback(test)

Expand Down
10 changes: 6 additions & 4 deletions green/test/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ def testPass(self):
)
fh.close()
module_name = basename + ".test_pool_runner_dotted.A.testPass"
result = Queue()
poolRunner(module_name, result, 1)
result.get()
self.assertEqual(len(result.get().passing), 1)
results = Queue()
poolRunner(module_name, results, 1)
results.get()
result = results.get()
self.assertEqual(len(result.passing), 1)
self.assertGreater(float(result.test_time), 0)

def test_SyntaxErrorInUnitTest(self):
"""
Expand Down
10 changes: 10 additions & 0 deletions green/test/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ def test_displayStderr(self):


class TestProtoTestResult(unittest.TestCase):
def test_startStop(self):
"""
startTest/stopTest work correctly
"""
ptr = ProtoTestResult()
test = proto_test(MagicMock())
ptr.startTest(test)
ptr.stopTest(test)
self.assertGreater(float(ptr.test_time), 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For readability it is better to use plain words:

Suggested change
ptr = ProtoTestResult()
test = proto_test(MagicMock())
ptr.startTest(test)
ptr.stopTest(test)
self.assertGreater(float(ptr.test_time), 0)
result = ProtoTestResult()
test = proto_test(MagicMock())
result.startTest(test)
result.stopTest(test)
self.assertGreater(float(result.test_time), 0)

That said I have a bit of concern about this because time is not mocked and on a future very fast machine it would not be impossible for the time to be recorded as a true zero.

We could add https://pypi.org/project/freezegun/ in our requirements-dev.txt file and use it to mock the time to ensure that we have predictable behavior.

Copy link
Contributor Author

@eltoder eltoder Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to be consistent with other tests. This is the naming and the structure used in all other tests in TestProtoTestResult.

I don't think we need to worry about time being zero. Even calling time.time() in a tight loop does not produce the same number. If it does become a problem, we can simply add a small sleep in the test. freezegun sets time to a constant, which will result in test_time being zero. If we need to mock, we can directly mock time.time() to an iterable, so it returns different values on subsequent calls.

@CleanCut what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added mocking for time.time() and a more precise assert.


def test_addSuccess(self):
"""
addSuccess adds a test correctly
Expand Down