This may be more of a theoretical question than a practical one, but I'd like to know the community's opinion.
Some context:
On some integration UI test, in which we are testing that a field appearing on a specific area is actually showing the expected value that is calculated on backend, so for example, for an input like:
Consider Value Probability
---------------------------------
N 100 20%
Y 100 10%
Y 200 15%
The system will show on a field of the page those three lines in a specific format, and in another field:
Total: 40
(notice 40 = 100*10% + 200*15%, first line is set to not be considered)
In the scope of a selenium test that is creating those initial lines onto database via preconditions, what would you consider a good practice and what a bad practice?
A) Do the assertion on, literally, the "magic string" 40.
createPreconds();
assert(totalField.getValue()).equals(40);
B) Do the assertion against a calculation done on the test.
createPreconds();
assert(totalField.getValue()).equals(calculatedExpected());
float calculatedExpected(){
// get all the created preconditions, and compute what should be the correct output
return number
}
It is to be mentioned that the input is actually much more complex than what I'm explaining, and its logic may change depending on the state of the system (i.e., first column may or may not exist).
Question in short, is: which would you say it's a better approach? I personally see problems on both approaches:
Option A is using "magic strings", option B is pretty much duplicating the behavior of the code to be tested, neither of them usually considered good practices.
Option A is straight forward testing UI displaying the value and does not pollute automation code. Option B makes the test more resilient to changes, as well as for people in the future understanding why that value is that value.
Apologies for the long question, and thank you in advance