Reputation: 1172
I have read that assume
will not run the test if assumption failed,
but I am not sure regarding the logic of when to place assert
vs assume
.
For example: any resource loading check should be done with assume
?
When should I use assume
over assert
?
(Note: i am looking for correct design of when to use one over the other)
Upvotes: 33
Views: 23058
Reputation: 61959
As you already know, you use assert
to fail a test if something goes wrong.
You use assume
if you have circumstances under which a test should not run. "Not run" means that it cannot fail, because, well, it did not run.
So, in a hypothetical scenario where:
you would write a test which:
Technically, both assert
and assume
throw exceptions. The difference is that:
assert
will cause the test to fail, whileassume
will cause the test to be skipped.Upvotes: 43
Reputation: 51
The most easiest difference between Assert and Assume is :
Assume will only run when the assumption is true. Will be skipped if it false.
assumeTrue(boolean assumption, String message)
Assert will run normally if true. In case of false assert, it gives predefined error message.
assertTrue(boolean condition, String message)
Upvotes: 5
Reputation: 849
The Assert class is the workhorse of JUnit and is the class JUnit testers are most familiar with. Most JUnit assert signatures are similar in nature. They consist of an optional message, an expected instance or variable and the actual instance or variable to be compared. Or, in the case of a boolean test like True, False, or Null, there is simply the actual instance to be tested.
The signature with a message simply has an initial parameter with a message string that will be displayed in the event the assert fails:
assert<something>(“Failure Message String”, <condition to be tested>);
Assumptions: You’ve probably heard that it’s best not to work on assumptions so here is a testing tool JUnit gives you to ensure your tests don’t.
Both Asserts and Assumes stop when a test fails and move on to the next test. The difference is that a failed Assert registers the failure as a failed test while an Assume just moves to the next test. This permits a tester to ensure that conditions, some of which may be external and out of control of the tester, are present as required before a test is run.
There are four varieties of Assumes: one to check a boolean condition, one to check that an exception has not occurred, one to check for null objects, and one that can take a Hamcrest matcher. As seen in the Assert section above, the ability to take a Hamcrest matcher is a gateway to testing flexibility.
You can read more here https://objectcomputing.com/resources/publications/sett/march-2014-junit-not-just-another-pretty-assert/
In short Assume used to disable tests, for example the following disables a test on Linux: Assume.assumeFalse(System.getProperty("os.name").contains("Linux"));
Assert is used to test the functionality.
Upvotes: 12
Reputation: 140417
Simply check out the javadoc for Assume:
A set of methods useful for stating assumptions about the conditions in which a test is meaningful. A failed assumption does not mean the code is broken, but that the test provides no useful information.
In other words: when an assert fires, you know that your testcase failed. Your production code isn't doing what you expect it to do.
Assume means ... you don't know exactly what happened.
Upvotes: 3