Dart - How run a function after or before each test? - dart

I'm using the Dart test package: https://pub.dartlang.org/packages/test
Often, I want to run some function before or after each tests in my test file. Does the test package provide something for this?

add a setUp(() { add your code here}) before your test() function.
There is also a tearDown() which is run after each test.
If you add the setUp function at top level in main it is run for every test, if you put it inside a group it is run for every test in that group. You can have setUp/tearDown on more than one level at the same time.
tearDown is executed in any case (like finally) no matter if the test fails or succeeds.
Recently setUpAll() and tearDownAll() was added to do some set up and tear down once before and after all tests.

Related

Incapable of running unit tests because of CREATE INDEX and CREATE CONSTRAINT

I've developed a module for Neo4j using Graphaware package. As a part of my module, I want to make sure that some indices and/or constraints are present in the database. To this end, I use BaseTxDrivenModule.initialize method to run a couple of Cypher statements:
#Override
public void initialize(GraphDatabaseService database)
{
database.execute("CREATE CONSTRAINT ON (n:`Label`) ASSERT n.`id` IS UNIQUE;");
database.execute("CREATE INDEX ON n:`Label2`(`id`) IS UNIQUE;");
}
These statements run successfully in production when I deploy the module in a server instance of Neo4j. But when I want to run the unit tests, as a part of build process, the execution hangs and never finishes. And when I omit the initialize method, it goes on without any error.
The worst part is that I have to build the package like: mvn package -Dmaven.test.skip=true or it won't build anything.
So my question is, why? And how can I fix this problem?
Here's a sample project demonstrating the issue:
https://github.com/ziadloo/test_neo4j_module
Just clone it and run mvn package and you'll see that the tests never finish.
There is no guarantee that the Runtime is started during your test, you'll have to assert it by invoking the waitUntilStarted method.
#Before
public void setUp() {
database = new TestGraphDatabaseFactory()
.newImpermanentDatabaseBuilder()
.loadPropertiesFromFile(this.getClass().getClassLoader().getResource("neo4j-module.conf").getPath())
.newGraphDatabase();
getRuntime(database).waitUntilStarted();
registerShutdownHook(database);
}
I would suggest you take a look at some test cases in the neo4j-uuid module for eg.

Grails Test Suite - Specify test order

I am trying to get my geb-spock functional tests to run in a specified order because SpecA will create data required for SpecB during its run.
This question is about running the specifications in order, not the individual test methods within the specification.
I have tried changing the specification name to indicate execution order but that didn't work. I found a solution where a Test Suite was used, and the tests were added to the suite in order, but I can't find how to make a test suite work in Grails.
Explicitly specifying them as grails test-app functional: SpecA SpecB , is not a long term option, as more specs will be added.
For sequential or whatever the sequence you want to run your tasks, I do the following thing in my build.gradle file:
def modules = ["X", "Y", "Z", "ZZ"]
if (modules.size() > 1) {
for(j in 1 .. modules.size()-1 ) {
tasks[modules[j]].mustRunAfter modules[values[j-1]]
}
}
Hope that helps. Cheers!
Not really an answer to your question but a general advice - don't do this. Introducing data setup dependencies between test classes will make your suite brittle in the long run. Reasoning about what the state is at a given point will get harder and harder as the amount of tests grows and the global state size with it. Later on hanging a test or introducing a new one might break many tests downstream. This is just asking for trouble.
Ideally, you want to setup the data needed by a test immediately before that test and tear it down afterwards. Grails Remote Control plugin and test data fixture builders are your friends here.
You should define your initialization code in a single place, and if it's shared between both Specs, it may be a good idea to create a superclass with methods you can call in each Spec's set up methods, or a whole class devoted to declare testing methods to reuse.
In any case, the purpose of a unit test is only to test a single functionality, and it shouldn't be responsible of setting up other tests as well.

How to set the transactionnal option per test class in a spock integration test?

In a grails project (version 2.3.7) , i have an integration test using Spock :
class SimpleIntegrationTests extends IntegrationSpec{
void "test an action from controller to database"(){...}
}
This integration test launch a batch with Spring Batch. Spring batch does not accept when a batch is launched from an existing transaction :
java.lang.IllegalStateException: Existing transaction detected in JobRepository. Please fix this and try again
So i tell my integration test to run without transaction, with :
static transactional = false
Doing this, the test runs with success.
But there are other integration tests in my project, which need transactions to run correctly.
And the instruction "transactionnal = false" in my test class is not confined to this test class, but affects all other integration tests triggered after my test class.
I understood there is an alphabetically execution order for the tests. So i know that if i rename my test class to be the last test class to run, it works fine, and all my tests are successful. But i think it s not an ideal answer to the problem.
So my question is : how to define that a test class is non transactionnal, in a manner that does not affect other integration tests ?
I know this is an old question and I imagine you've found a solution already, but as a work around I think you need to explicitly specify static transactional = true on integration tests that you still want to be transactional. We've had a similar issue with our Grails 2.3.7 test suite...

Is it possible to use a method in a Unit Test file in Rails Console?

I have a unit test method in my test/unit/model_test.rb :
def valid_sendRequestXML(account)
Hpricot.XML <<-XML
<ticket>#{account.api_token}</ticket>
XML
end
And I'm trying to manually run some things in my console to see what is failing where. Is it possible to somehow call this method within console? If so, how?
You can require unit test files just like any other Ruby file. The unit tests are methods of a class, so you can instantiate the testcase and run the methods you'd wish to run.
Also consider using pry. Just put binding.pry in your unit test, run it will open a console for you to inspect all the local variables (and other stuff) in.

Fitnesse: how to initialize/tearDown suite (not individual test)

I have a basic question:
When using JUnit, in your test suite (the class that holds your tests) you can declare suite-level initialization and cleanUp behaviour using #BeforeClass and #AfterClass. The methods thusly annotated are called before the suite begins and after the suite finishes all its tests respectivelly. This differs from the methods inside the suite which are annotated with #Before and #After which are called before each test is launched and after each finishes respectivelly.
I want the same setup in Fitnesse. I found out how to have stuff that's executed before and after each test (you create a SetUp page and a TearDown page in the suite, and they'll be called before and after each test in the suite). These are the equivalent of #Before and #After in JUnit. I can't however understand how can I declare a suite-level initialization and cleanUp logic (like #BeforeClass and #AfterClass in JUnit). Can someone please tell me if/how this can be achieved?
What I've tried is have a parent suite with SetUp and TearDown pages, and INSIDE that parent suite have mutlipl child suites which contain the actual tests (each with it's own SetUp and TearDown), hoping that this way the parent suite SetUp and TearDown pages will be called before and after each child suite, and the child suite's SetUp and TearDown pages will be called before and after each test in the suite, but no dice, what happends here is that the child suite SetUp/TearDown overrides the parent suite ones.
SuiteSetUp and SuiteTearDown - see http://www.fitnesse.org/FitNesse.FullReferenceGuide.UserGuide.WritingAcceptanceTests.SpecialPages

Resources