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

use better test assertions by replacing use of assertFalse/assertTrue #4170

Merged
merged 3 commits into from
Jan 18, 2023

Conversation

Flamefire
Copy link
Contributor

@Flamefire Flamefire commented Jan 9, 2023

Replace self.assertTrue(False, ... and similar by appropriate assert functions. This avoids redundancy and potential mistakes and improves error reporting.

I checked that all functions are available in Python 2.7 and 3.1+

Especially the assertTrue(False were annoying as the error message started with "Assertion failed: False is not True"

Most of the assert[Not]In replace the need to duplicate the arguments in the message which sometimes got forgotten.

Mostly done by a RegEx search&replace so should be save.

I also fixed a bug in the test:

mods = ['GCC/6.4.0-2.28', 'hwloc/1.11.8-GCC-6.4.0-2.28', 'OpenMPI/2.1.2-GCC-6.4.0-2.28']
self.assertTrue([m['mod_name'] for m in self.modtool.list()], mods)

Here assertEqual was intended as shown by the 2nd parameter. Another reason why we should try to avoid the generic assertTrue/False tests

@Flamefire Flamefire force-pushed the better-test-asserts branch from 77a99a8 to 6c5b4c3 Compare January 9, 2023 11:45
Replace `self.assertTrue(False, ...` and similar by appropriate assert functions.
This avoids redundancy and potential mistakes and improves error reporting.
LMod ignores $LC_ALL and uses $LANG so set that too for the tests.
The output of running `thisshouldfail` may be "bash: line 1: thisshouldfail: command not found"
depending on the bash version, so adjust the regex
`assertTrue(somelist, otherlist)` will not trigger a failure when it should as
`assertEqual` was intended
@Flamefire Flamefire force-pushed the better-test-asserts branch from 6c5b4c3 to cbcfdf7 Compare January 10, 2023 08:42
@easybuilders easybuilders deleted a comment from boegelbot Jan 18, 2023
@boegel boegel added this to the next release (4.7.1?) milestone Jan 18, 2023
Copy link
Member

@boegel boegel left a comment

Choose a reason for hiding this comment

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

@Flamefire Thanks a lot for the effort here, makes a lot of sense.

Before I start wading through this to make sure we're not accidentally introducing a check that always passes, I think it makes sense to prevent that we re-introduce the use of self.assertFalse or self.assertTrue.

For this, we should add a test in test/framework/general.py that searches for the use of these, and fails if it finds any?
That can be done by iterating over all test/framework/*.py files, and using "\s+self\.assert(False|True)" as regex, I think...

edit: nevermind, not all assertFalse/assertTrue are removed here, so that won't work...

@boegel boegel changed the title Use better test assertions use better test assertions by replacing use of assertFalse/assertTrue Jan 18, 2023
@Flamefire
Copy link
Contributor Author

Before I start wading through this to make sure we're not accidentally introducing a check that always passes, I think it makes sense to prevent that we re-introduce the use of self.assertFalse or self.assertTrue.

There are cases where those make sense. E.g. one compares the GITHUB_TOKEN and purposely is not using assertEqual to avoid it being printed. So a check like that is not possible.

Of course we could use # noqa style comments to annotate such lines. But I'd say that's a bit to much...

Other solution: Switch to pytest and use assert foo == bar instead. No need to worry about the test function to use ;-) But that would be a larger effort.

@boegel
Copy link
Member

boegel commented Jan 18, 2023

There's definitely advantages in a switch to pytest, but yes, that would be a big effort.
Although that's probably something that could be done gradually...

It would be nice if we could outline some "best practices" in the EasyBuild documentation w.r.t. writing tests, to add to https://docs.easybuild.io/en/latest/Unit-tests.html

@boegel boegel mentioned this pull request Jan 18, 2023
2 tasks
Copy link
Member

@boegel boegel left a comment

Choose a reason for hiding this comment

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

lgtm

@boegel
Copy link
Member

boegel commented Jan 18, 2023

Spent some time scrolling through this, didn't spot anything suspicious that would require changes, so good to go.

Thanks a lot for the effort here @Flamefire!

@boegel boegel merged commit e9b7cea into easybuilders:develop Jan 18, 2023
@Flamefire Flamefire deleted the better-test-asserts branch January 19, 2023 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants