How can I see the error messages of assumeThat in JUnit4?
This test passes but does not print anything. The reason is that to perform the test on read1, I require b to be true. Since b is false, the test does not apply.
boolean b = false;
byte[] read1 = null;
assumeThat("b shold be true to continue testing", b, is(true));
assertThat("read1 should not be null", read1, is(notNullValue()));
I have not found any kind of log with the assumeThat messages. My issue here is that I would like to know which tests passed but did not completed because assumption X failed. It would be nice if a message (for example similar to when an assertion fails) was still printed for the assumptions.
Strangely as it may seem, I haven't been able to find comments on this problem (including in the JUnit page). So there is a chance that I'm just doing something wrong or looking for messages in the wrong place. I'm using Eclipe.
If I understand you correctly, the
assertThat("read1 should not be null", read1, is(notNullValue()));
should only be executed if b == true, but it's not an error if b == false?
Then why don't you simply do:
if(b) {
assertThat("read1 should not be null", read1, is(notNullValue()));
} else {
log.warn("b == false, Test on read1 skipped");
}
? Or do you want to have a special test result state ("success-but-suspicious") if that happens?
Update: I just re-read the docs on Assume. It states that if an assumtion fails, the entire test is considered "not meaningful" and marked as "ignored". With small, focused tests that should usually give you all the info you need.
Update2: Just tried it with the JUnit 4.8 in Eclipse 4.2 I have on hand here. That just marks the test as successful. So I guess the JUnit runner in Eclipse might not support that feature correctly.
An error isn't printed because an assumption failure isn't an error. The assume methods are used to short-circuit tests that should not be run due to the value or state one or more objects (or primitives). Assumptions were designed for the Theories runner but they can be used in other runners as well.
One common usage of assumptions is when you have multiple test methods that need to perform some set of operations to get an object into a particular state before the actual test case logic can be performed.
Before assumptions, each test would get the object into a desired state, assert that it is in that state, then continue. For example, for a test of a stack, many tests require a non-empty stack, so you would create a stack, push an item, assert the stack is not empty, then do the rest of the test (pop an item, push another item, etc).
The problem comes if you do this pattern in multiple tests, and then a bug gets introduced that causes all of these tests to fail (for example, isEmpty() always returning true). When you run the tests you get so many failures that you don't know where to start.
So instead you have one test that verifies that if you push an item into an empty stack, isEmpy() returns false. Then any test that needs a non-empty stack does this:
Stack<Object> stack = new Stack<>();
stack.push(new Object());
assumeFalse(stack.isEmpty());
// Continue with the test methods
If you want an error message, you probably want to use an assertion.
Agreeing with the previous answer (by NamshubWriter).
However, if you really want (justifiably) to have a clear indication that the test was ignored because some assumption failed, you can do something like:
#Before
public void before() {
final String mandatoryPropKey = "system.mandatory-property-for-this-test";
final String mandatoryPropExpectedVal = "true";
try { // try/catch because the Assume failure won't print any message (by design)
Assume.assumeTrue("Assumed that the \"" + mandatoryPropKey + "\" system property is \"" + mandatoryPropExpectedVal + "\".", Boolean.valueOf(System.getProperty(mandatoryPropKey, mandatoryPropExpectedVal)));
} catch (AssumptionViolatedException ave) {
logger.warn("Not executing this test because this assumption failed: {}", ave.getMessage());
throw ave;
}
}
Related
I have a Protractor test that pulls various values from the UI and stores them as variables for comparison with values from a different database.
Now this test needs to run against multiple sites BUT of the 25 maximum data points recorded, some sites only have 22.
Clearly the test fails on those "22" sites since the elements are not present.
What I want to achieve is where there's a "22" site, the tests against the not present elements are ignored and the test proceeds to the end. Conveniently, the "missing" elements are the last ones in the spec.
Crudely speaking...
if element-y is not present end test or if element-y is present continue
Grateful if anyone could advise.
Thanks #sergey. I've modified your example as below....
if (!(await element(by.xpath('//*[#id="root"]/div/div[2]/main/div/div/section[5]/div/div/div[1]/section/div/span')).isPresent())) {
console.warn ('Functions are not present, closing the session')
await browser.close()
I get this error:
if (!(await element(by.xpath('//*[#id="root"]/div/div[2]/main/div/div/section[5]/div/div/div[1]/section/div/span')).isPresent())) {
^^^^^^^
SyntaxError: Unexpected identifier
I've tried using a 'var' instead of the actual element, but get the same result.
Thanks
well the best option that I recall is still pretty dirty... you can do something like this
if (!(await element.isPresent())) {
console.warn('Element not present, closing the session')
await browser.close()
}
And then the rest of test cases will fail as session not found or similar error
The reason you can't do anything better because in protractor you can't do conditional test cases based on a Promise-like condition, if that makes sense...
Ok, I've been stuggling with this one for a while, and have spent a lot of time trying different things to do something that I have done very easily using PHP.
I am trying to iterate over a list while keeping track of a variable locally, while spitting out HTML attempting to populate a table.
Attempt #1:
#{
var curDate : Date = null
for(ind <- indicators){
if(curDate == null || !curDate.equals(ind.getFirstFound())){
curDate = ind.getFirstFound()
<tr><th colspan='5' class='day'>#(ind.getFirstFound())</th></tr>
<tr><th>Document ID</th><th>Value</th><th>Owner</th><th>Document Title / Comment</th></tr>
}
}
}
I attempt too user a scala block statement to allow me to keep curDate as a variable within the created scope. This block correctly maintains curDate state, but does not allow me to output anything to the DOM. I did not actually expect this to compile, due to my unescaped, randomly thrown in HTML, but it does. this loop simply places nothing on the DOM, although the decision structure is correctly executed on the server.
I tried escaping using #Html('...'), but that produced compile errors.
Attempt #2:
A lot of google searches led me to the "for comprehension":
#for(ind <- indicators; curDate = ind.getFirstFound()){
#if(curDate == null || !curDate.equals(ind.getFirstFound())){
#(curDate = ind.getFirstFound())
}
<tr><th colspan='5' class='day'>#(ind.getFirstFound())</th></tr>
<tr><th>Document ID</th><th>Value</th><th>Owner</th><th>Document Title / Comment</th></tr>
}
Without the if statement in this block, this is the closest I got to doing what I actually wanted, but apparently I am not allowed to reassign a non-reference type, which is why I was hoping attempt #1's reference declaration of curDate : Date = null would work. This attempt gets me the HTML on the page (again, if i remove the nested if statement) but doesn't get me the
My question is, how do i implement this intention? I am very painfully aware of my lack of Scala knowledge, which is being exacerbated by Play templating syntax. I am not sure what to do.
Thanks in advance!
Play's template language is very geared towards functional programming. It might be possible to achieve what you want to achieve using mutable state, but you'll probably be best going with the flow, and using a functional solution.
If you want to maintain state between iterations of a loop in functional programming, that can be done by doing a fold - you start with some state, and on each iteration, you get the previous state and the next element, and you then return the new state based on those two things.
So, looking at your first solution, it looks like what you're trying to do is only print an element out if it's date is different from the previous one, is that correct? Another way of putting this is you want to filter out all the elements that have a date that's the same date as the previous one. Expressing that in terms of a fold, we're going to fold the elements into a sequence (our initial state), and if the last element of the folded sequence has a different date to the current one, we add it, otherwise we ignore it.
Our fold looks like this:
indicators.foldLeft(Vector.empty[Indicator]) { (collected, next) =>
if (collected.lastOption.forall(_.getFirstFound != next.getFirstFound)) {
collected :+ next
} else {
collected
}
}
Just to explain the above, we're folding into a Vector because Vector has constant time append and last, List has n time. The forall will return true if there is no last element in collected, otherwise if there is, it will return true if the passed in lambda evaluates to true. And in Scala, == invokes .equals (after doing a null check), so you don't need to use .equals in Scala.
So, putting this in a template:
#for(ind <- indicators.foldLeft(Vector.empty[Indicator]) { (collected, next) =>
if (collected.lastOption.forall(_.getFirstFound != next.getFirstFound)) {
collected :+ next
} else {
collected
}
}){
...
}
I have a simple mongo application that happens to be async (using Akka).
I send a message to an actor, which in turn write 3 records to a database.
I'm using WriteConcern.SAFE because I want to be sure the write happened (also tried WriteConcern.FSYNC_SAFE).
I pause for a second to let the writes happen then do a read--and get nothing.
So my write code might be:
collection.save( myObj, WriteConcern.SAFE )
println("--1--")
collection.save( myObj, WriteConcern.SAFE )
println("--2--")
collection.save( myObj, WriteConcern.SAFE )
println("--3--")
then in my test code (running outside the actor--in another thread) I print out the # of records I find:
println( collection.findAll(...) )
My output looks like this:
--1--
--2--
--3--
(pauses)
0
Indeed if I look in the database I see no records. Sometimes I actually do see data there and the test works. Async code can be tricky and it's possible the test code is being hit before the writes happen, so I also tried printing out timestamps to ensure these are being executed in the order presented--they are. The data should be there. Sample output below w/timestamps:
Saved: brand_1 / dev 1375486024040
Saved: brand_1 / dev2 1375486024156
Saved: brand_1 / dev3 1375486024261
1375486026593 0 found
So the 3 saves clearly happened (and should have written) a full 2 seconds before the read was attempted.
I understand for more liberal WriteConcerns you could get this behavior, but I thought the two safest ones would assure me the write actually happened before proceeding.
Subtle but simple problem. I was using a def to create my connection... which I then proceeded to call twice as if it was a val. So I actually had 2 different writers so that explained the sometimes-difference in my results. Refactored to a val and all was predictable. Agonizing to identify, easy to understand/fix.
So I'm at a loss as to why this test fails. When I run the statements in the repl everything appears to work correctly but the fiveam test fails.
There is a test case in the following gist: https://gist.github.com/PuercoPop/5765844
the fiveam test fails with the following message. I don't understand why the second board is displayed differently (with new lines):
EXPECTED-BOARD evaluated to (:EMPTY :|2| :|3| :|4| :|5| :|6| :|7| :|8| :|9|),
which is not EQUAL to (:EMPTY
:|2|
:|3|
:|4|
:|5|
:|6|
:|7|
:|8|
:|9|)..
You are modifying constant data. Weird things are allowed to happen when you modify constant data. If there's even half a chance you'll be unleashing a destructive function (as in "modify the data...") create your lists using (list ...) instead of '(...).
i am writing a small perl app using the eXist database, and i am wondering is:
how can i see that my call
my $eXist = XML::eXistDB::RPC->new( destination=>$eXist_db, repository=>$bank, user=>"admin", password=>"pass" ) ;
is successful or not ?
thanx
When object initialisation fails, it will be messaged through Log::Report, so hook into that.
This only happens if the programmer to neglected to set either rpc or destination parameter. The new constructor will always return an object instance.
According to the docs:
All methods return a LIST, where the
first scalar is a return code (RC).
When that code is 0, all went well.
Otherwise, the code represent the
transport error or the exception
(refusal) as reported by the server
logic. In either case, the second
scalar in the returned list contains
the error message. For instance,
Maybe this applies also for the constructor, try:
my ($rc,$eXist) = XML::eXistDB::RPC->new( destination=>$eXist_db, repository=>$bank, user=>"admin", password=>"pass" );
now, if $rc != 0 there was an error.