Every now and then I run into a situation where I need to make absolutely sure that one test executes (successfully) before another one.
For example:
"The SecurityManager" should {
"make sure an administrative user exists" in new WithApplication with GPAuthenticationTestUtility {
checkPrerequisiteAccounts(prerequisiteAccounts)
}
"get an account id from a token" in new WithApplication with GPAuthenticationTestUtility {
val token = authenticate(prerequisiteAccounts.head)
token.isSuccess must beTrue
myId = GPSecurityController.getAccountId(token.get)
myId != None must beTrue
myId.get.toInt > 0 must beTrue
}
The first test will create the admin user if it doesn't exist. The second test uses that account to perform a test.
I am aware I can do a Before/After treatment in specs2 (though I've never done one). But I really don't want checkPrerequisiteAccounts to run before every test, just before that first test executes... sort of a "before you start doing anything at all, do this one thing..."
Anyone know if there is a way to tag a particular test as "do first" or "do before anything else?"
You can just add a "Step" in between tests to enforce some sequentiality:
"make sure an administrative user exists" in ok
step("admin is created".pp)
"get an account id from a token" in ok
You can also add sequential to your spec like, to sequential execution of tests.
class MySpec extends mutable.Specification {
sequential
// rest follows
behaviour one
behaviour two
}
Related
I am using ScalaTest for automation.
A typical test logical structure is that the test is doing some checks of the application logic and then cleans up. So let's call it a test body part and a test cleanup part. If the test body fails I would like to see it in the test report. If the test body does not fail but the cleanup part fails I also would like to see in the test report that the test ended up with error.
So I've come up with the following structure (example is the most simple I can provide):
"Admin" should "be able to create a new team" in{
val tempTeam = Team("Temp QA Team")
val attempt=Try{
When("Admin opens the Teams view")
TeamsPage.open
And("creates a new team")
TeamsPage.createNewTeam(tempTeam)
Then("this team is shown in the list")
TeamsPage.isParticularTeamShownInTeamList(tempTeam.name) shouldBe true
}
val cleanUp = Try(TeamsPage.cleanUpTeam(tempTeam))
attempt match{
case Failure(e) => throw e
case Success(r) =>{
if(cleanUp.isFailure) cleanUp.get
r
}
}
}
Please note here that I need the cleanup part to always execute, not only when the test body part is successful.
It works as I expect but I see two problems:
IntelliJ Idea tells me that cleanUp.get is useless expression. How to write that part in more correct way? I could rewrite it as if(cleanUp.isFailure) throw cleanUp.failed.get, then the IDE would not complain but actually that is a longer way to write the same statement.
The last part of this test code which actually compares results of the test body part and cleanup part and decides what to return looks a bit bloated. Probably you can advice me how to make it more concise and clear?
If I understand what you're trying to do correctly, the answer is flatMap and map as laid out in the documentation for scala.util.Try
In your case (taking your code as is), you would want
"Admin" should "be able to create a new team" in{
val tempTeam = Team("Temp QA Team")
val attempt=Try{
When("Admin opens the Teams view")
TeamsPage.open
And("creates a new team")
TeamsPage.createNewTeam(tempTeam)
Then("this team is shown in the list")
TeamsPage.isParticularTeamShownInTeamList(tempTeam.name) shouldBe true
}
val cleanUp = Try(TeamsPage.cleanUpTeam(tempTeam))
attempt.flatMap(r => cleanup.map(c => r)).get
}
This will return the result of attempt, unless it fails, in which case it will throws attempt's exception. It will ignore the successful result of cleanup (as your code did), but if cleanup throws an exception, you'll throw that exception.
N.B. I didn't actually try this in an IDE, so I can't say if this will address your question #1 about IntelliJ saying that get was a useless expression.
I execute a business rule from a process on Jbpm, the rule is simple:
package com.test.flow;
rule "sample"
ruleflow-group "test"
when
then
System.out.println("Hello World");
end
But, I don't no why, this rule execute only once, for instance, I run a new instance of the process and in the jbpm console print "Hello World", but, when I run a second instance of the process doesn print anymore "Hello World", some one can you help me? or tellme why does this happen?
Screen Jbpm console
For this kind of "hello-world" rule, using ruleflow-group is most likely just causing confusion. Do you control when this group is activated? How? - Omitting this rule attribute is indicated.
A rule with an empty left-hand-side will execute only once in a session (as Esteban pointed out).
If you want a rule that fires once for each inserted fact, use
rule "new fact"
when
Object()
then
System.out.println( "new fact inserted" );
end
I am developing a unit test project where I create an item in a test, then create sub items for it in the following test.
These tests are parameterized tests, and these parameters are collected in the runtime, so when the project starts it starts. It fails to retrieve the parent item from the database because they are not created yet "as I haven't run the first test yet".
Is there a workaround for this?
The first function:
[Test, Sequential]
public void AddInitiative([ValueSourceAttribute("Get_AddInitiatives_Data_FromExcel")]AddInitiative Initiative_Object)
{
string URL = "http://" + Server_name + Port_number + "/IntegrationHub/IntegrationHub.svc/RestUnsecure/AddInitiative";
string Token = Get_Security_token("gpmdev\\administrator", "Xyz7890", TenantID_Input);
var Response = POST_Request(Initiative_Object, URL, Token);
Guid Returned_GUID = GenericSerializer<Guid>.DeserializeFromJSON(Response);
DataBase_Queries DB = new DataBase_Queries();
List<StrategyItem> StrategyItemsFromDB=DB.GetStrategyItemByID(Returned_GUID.ToString());
Assert.AreEqual(Initiative_Object.Initiative.Name_En, StrategyItemsFromDB[0].Name_En);
}
The second function that fails:
[Test, Sequential]
public void AddInitiativeMilestones([ValueSourceAttribute("Get_AddInitiativeMilestones_Data_FromExcel")]AddMilestone Milestone_Object)
{
string URL = "http://" + Server_name + Port_number + "/IntegrationHub/IntegrationHub.svc/RestUnsecure/AddInitiativeMilestones";
string Token = Get_Security_token("gpmdev\\administrator", "Xyz7890", TenantID_Input);
var Response = POST_Request(Milestone_Object, URL, Token);
List<Milestone> Returned_Milestone = GenericSerializer<List<Milestone>>.DeserializeFromJSON(Response);
DataBase_Queries DB = new DataBase_Queries();
List<StrategyItem> StrategyItemsFromDB = DB.GetStrategyItemByID(Returned_Milestone[0].ID.ToString());
Assert.AreEqual(Milestone_Object.Milestones[0].Name_En, Returned_Milestone[0].Name_En);
Assert.AreEqual(Milestone_Object.Milestones[0].Name_En,StrategyItemsFromDB[0].Name_En);
}
Update: When I clicked from the GUI on Clear fixture the test data was reloaded, but it there a way to do that without the GUI?
It's generally bad practice in unit tests to have one test depend on (i.e. use output from) another test. In this case, with NUnit, it's actually impossible.
It's impossible because NUnit creates tests long before they are executed. NUnit will call your TestCaseSource methods at what we call "load time" when NUnit decides what tests exists and populates a GUI if you use one.
The code in your tests executes at "run time" for the tests. In a gui, this may happen multiple times for each load - every time you click Run, for example.
Note that I'm explaining this in terms of a GUI because it's an easy way to conceptualize it. NUnit works the same way whether you are running in batch or interactively.
If you want something to happen only once, before any tests are run, you can use OneTimeSetUp (TestFixtureSetUp in NUnit V2) to set it up. You can use a member of the class to save whatever you need from that execution and access it from your tests. However, this will still happen at "run time", decades (in computer terms) after your tests have been loaded.
I'm trying a simple sequence of tests on an API:
Create a user resource with a POST
Request the user resource with a GET
Delete the user resource with a DELETE
I've a single frisby test spec file mytest_spec.js. I've broken the test into 3 discrete steps, each with their own toss() like:
f1 = frisby.create("Create");
f1.post(post_url, {user_id: 1});
f1.expectStatus(201);
f1.toss();
// stuff...
f2 = frisby.create("Get");
f2.get(get_url);
f2.expectStatus(200);
f2.toss();
//Stuff...
f3 = frisby.create("delete");
f3.get(delete_url);
f3.expectStatus(200);
f3.toss();
Pretty basic stuff, right. However, there is no guarantee they'll execute in order as far as I can tell as they're asynchronous, so I might get a 404 on test 2 or 3 if the user doesn't exist by the time they run.
Does anyone know the correct way to create sequential tests in Frisby?
As you correctly pointed out, Frisby.js is asynchronous. There are several approaches to force it to run more synchronously. The easiest but not the cleanest one is to use .after(() -> ... you can find more about after() in Fisby.js docs.
This question may have a bit of philosophical aspect to it.
I have been using Deadbolt 2 (Scala) in my Play application and it works quite well.
In looking at the Restrict function definition (line 47) I noticed that it will invoke the onAuthFailure for one of the following reasons:
No user in session (no subject)
Action specified no roles.
User attempted an action for which they did not possess one or more required roles.
In my application UI, I would like to receive a different status code for each of these so that a user that is not logged in (condition 1) will be redirected to login page but condition 3 would be more gracefully handled by just a warning (since they can do no harm anyway and might have accidentally tried to edit when they have 'read-only' access - perhaps a UI bug, but logging in again is a bit draconian).
If I had to settle for just 2 status codes, however, I would want to differentiate between 1 and the other 2. I can see how this could be accomplished but would like to get other opinions on the merits of even doing this.
If I were to implement this change, it looks like I could just override the Restrict function in my own extension of the DeadboltActions trait.
I'm a little new to scala, so I'm open to additional ideas on how to best accomplish these goals.
I decided to just add the code to differentiate between condition 1 and either 2 or 3 as follows:
In MyDeadboltHandler:
class MyDeadboltHandler(dynamicResourceHandler: Option[DynamicResourceHandler] = None) extends DeadboltHandler {
...
def onAuthFailure[A](request: Request[A]): Result = {
Logger.error("authentication failure")
val json = new JsonStatus("Failed to authenticate", -1).toJson.toString
if(noUserInSession(request)){
Results.Forbidden(json).withHeaders("Access-Control-Allow-Origin" -> "*")
}
else{
Results.Unauthorized (json).withHeaders("Access-Control-Allow-Origin" -> "*")
}
}
def noUserInSession(request:RequestHeader) = {
username(request) match {
case Some(u:String) => false
case _ => true
}
}
This works well for me and does not impose upon the basic Deadbolt-2 functionality.