I'm getting started with DbUnit lately and I'm trying to write a very simple integration test just to populate a table with 3 rows. Reading the DbUnit Getting Started Guide, it tells me to create a dataset file. My dataset xml file looks exactly like this:
<dataset>
<notaFiscal cliente="Cliente 1" valor="26.5" data='2016-04-04'/>
<notaFiscal cliente="Cliente 2" valor="30.5" data='2016-05-01'/>
<notaFiscal cliente="Cliente 3" valor="28.2" data='2015-08-11'/>
</dataset>
Then, I have to create a test class which extends DBTestCase and implement my test methods (annotated with #Test, like any other JUnit test case). The class I created is as follows:
public class GerenciadorNFTest extends DBTestCase {
private GerenciadorNotaFiscal gerenciador = new GerenciadorNotaFiscal();
public GerenciadorNFTest(String name)
{
super( name );
// PBJDT is an abbreviation of PropertiesBasedJdbcDatabaseTester
// just for a better visualization
System.setProperty(PBJDT.DBUNIT_DRIVER_CLASS,
"org.postgresql.Driver" );
System.setProperty(PBJDT.DBUNIT_CONNECTION_URL,
"jdbc:postgresql://localhost:5432/dbunit" );
System.setProperty(PBJDT.DBUNIT_USERNAME, "postgres" );
System.setProperty(PBJDT.DBUNIT_PASSWORD, "123456" );
}
protected IDataSet getDataSet() throws Exception {
IDataSet dataSet = new FlatXmlDataSetBuilder().build(
new FileInputStream("notas_fiscais.xml"));
return dataSet;
}
#Test
public void geraPedido() {
Pedido p = new Pedido("Diogo", 26d, 5);
gerenciador.gera(p);
NotaFiscal notaFiscal = gerenciador.recupera("Diogo");
Assert.assertEquals(notaFiscal.getCliente(), "Diogo");
}
}
After that, I tried to run the test case but got the following error:
junit.framework.AssertionFailedError: No tests found in teste.GerenciadorNFTest
at junit.framework.Assert.fail(Assert.java:57)
at junit.framework.TestCase.fail(TestCase.java:227)
If I tried to remove the extend DBTestCase, JUnit recognize the test case and runs normally, but with the extend it didn't. I tried to clean and re-compile, but it didn't work. I also tried to run the test outside the IDE I use (Intellij Idea) but again I had no sucess.
Has anyone been through this same problem?
Thank you very much in advance. Any help will be appreciated.
There are JUnit 3 vs 4 runner differences that may be the cause (you don't mention JUnit and dbUnit versions, nor how dependency managing them). And different tooling has different running default requirements (e.g. Maven defaults to only running classes as tests with class name suffix of "Test").
Note that it is not required to extend a dbUnit class (I don't) and not doing so should eliminate the encountered problem. Just further down that page you mentioned are two sections describing how:
Using your own parent class or a dbUnit IDatabaseTester instance: http://dbunit.sourceforge.net/howto.html#Database_setup_with_your_own_TestCase_subclass
Using no parent class: http://dbunit.sourceforge.net/howto.html#Database_setup_with_no_parent_class
And combining both is what I've done for years - have my own parent test class for common stuff and then DI (or instantiate) the desired DBTestCase (usually PrepAndExpectedTestCase).
Related
So I'm getting this persistent error using netbeans. I've got a LinkedList class which I am testing via a JUnit test, which I created by clicking on LinkedList.java: Tools -> Create/Update Tests and this LinkedListTest.java class is now located in test packages.
My LinkedList.java file works correctly when tested in a file with a main method.
public class LinkedListTest {
#Test
public void testAddFirst() {
LinkedList linkedList = new LinkedList();
Country c1 = new Country("Australia");
linkedList.addFirst(c1);
assertEquals("Australias", linkedList.getValue(0)); // Should fail a test
} // default test methods beneath
All my imports check out. JUnit 5.3.1 and I had to download apiguardian1.1.0.jar from MVN repository to clear an error for:
reason: class file for org.apiguardian.api.API$Status not found
I right-click in this file and select Test File, or use Ctrl+F6, I've selected Test File from the original LinkedList file, I've even used Alt+F6 which tests the whole project. Yet I'm met with 'No tests executed.', an empty Test Results window, and no Notifications. What am I doing wrong?
Thanks for any help
Edit: I just switched from netbeans to eclipse.
You forget to extend Runner with class --
use like below with class -
public class LinkedListTest extends Runner {
}
Hope this help you.
I'm wondering is it possible to make TestNG show in it's report something like nested tests or test steps.
The thing is that I have pretty big integration test cases and it would be nice if I could decouple it on some steps, say, with something like allure's #Step annotation.
For now the test case is shown as one huge separate entry in intellij IDEA report with a lot of logs which is very difficult to go through and analyze.
You can use createNode method of ExtentTest class. it will creating node under main test.
I've never tried to use Inner TestNG classes but that might be possible and I do believe IntelliJ would render the nested view. You could make a simple little project to try it out. There is a guy who seems to have tried inner classes here: http://makeseleniumeasy.com/2018/05/13/testng-tutorials-12-how-to-run-inner-testng-class-from-testng-xml/ . Let me know if it works. Won't know if IntelliJ renders the nesting until you try.
Bonus: If that works, would be interesting to execute your TestNG using Gradle testng plugin and see if HTML report also renders the nesting.
Inner classes can be used to form groups. For one level of nesting the inner classes must be made public.
Try the following:
public class foo {
public class bar1 {
#Test
public void test11() {System.out.println("test11");}
#Test
public void test12() {System.out.println("test12");}
}
public class bar2 {
#Test
public void test21() {System.out.println("test21");}
#Test
public void test22() {System.out.println("test22");}
}
}
JUnit does have #Nested to group them and you can use the shortcut cmd+R etc. to run them, especially if you have lots of test cases
For TestNG, either split them into different (inner) classes, or use structure view in IntelliJ (https://www.jetbrains.com/help/idea/viewing-structure-of-a-source-file.html)
Right-click all those tests that you want to run
Now you can use groups feature of TestNG E.g.:
class RecordsAccessorTest {
#BeforeMethod(groups = "NewRecordGroup")
public void setUp() {
// some set up for only new record creation is tested
}
#Test(groups = "NewRecordGroup")
public void testNewRecordCreation_happyPath() {...}
#Test(groups = "NewRecordGroup")
public void testNewRecordCreation_errorPath() {...}
#Test
public void someOtherTestNotInGroup() {...}
}
I am running into trouble with JUnit 5 (5.0 or 5.1) and custom extension.
We are using service loader to load all implementations which then modify how our extension is bootstrapped. These implementations can be loaded just once, so I was thinking of using ExtensionContext.Store and placing it there. Every subsequent test instance would then just load it from Store instead of via service loader.
Now, I am even aware of the hierarchical context structure and I know that there is some "root" context which you can get through ExtensionContext.getRoot(). But this "root" context (instance of JupiterEngineExtensionContext) isn't really root - there is different one for every test instance.
Say you have FooTest and BarTest, then printing out getRoot() for each of them yields:
org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext#1f9e9475
org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext#6c3708b3
And hence trying to retrieve previously stored information from Store fails.
Is having this limitation intended? It makes the borderline between ClassExtensionContext and JupiterEngineExtensionContext pretty blurred.
Is there another way to globally store some information via extension?
Here is a (very) simplified version of how I tried working with the store (cutting out all other information basically). I also added some System.out.print() calls to underline what I am seeing. Executing this extension on two test classes results in what I described above:
public class MyExtension implements BeforeAllCallback {
#Override
public void beforeAll(ExtensionContext context) throws Exception {
System.out.println(context.getRoot());
if (context.getRoot().getStore(Namespace.create(MyExtension.class)).get("someIdentifier", String.class) == null) {
context.getRoot().getStore(Namespace.create(MyExtension.class)).put("someIdentifier", "SomeFooString");
} else {
// this is never executed
System.out.println("Found it, no need to store anything again!");
}
}
}
EDIT: Here is a minimal project on GH(link), run by mvn clean install, which displays the behaviour I see.
I just copied your MyExtension verbatim (i.e., with zero changes) and ran both FooTest and BarTest.
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
#ExtendWith(MyExtension.class)
class FooTest {
#Test
void test() {
}
}
and
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
#ExtendWith(MyExtension.class)
class BarTest {
#Test
void test() {
}
}
And the result is:
org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext#2280cdac
org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext#2280cdac
Found it, no need to store anything again!
Thus, getRoot() works as documented.
The only explanation for why you see two different roots is that you must be executing the tests in different processes.
Please keep in mind that the root ExtensionContext instance is bound to the current execution of your test suite.
So if you run FooTest and BarTest one after the other in an IDE, that will actually result in two "test suites" with different roots. The same is true if you configure your build tool to fork between test classes.
Whereas, if you execute both test classes together in a single "test suite" (e.g., by telling your IDE to run all tests in the same package or same source tree) you will then see that there is one root like in the output I provided above.
Note, however, that there was an issue with the junit-platform-surefire-provider prior to version 1.0.3, whereby the provider launched the JUnit Platform for each test class. This would give the appearance of forking even though Surefire did not actually start a new JVM process. For details, see https://github.com/junit-team/junit5/pull/1137.
So far, beside Junit and Jenkins, I used log4j to textual reporting results of test cases but now I want to use Allure, for generating clearer and more transparent reports from my selenium tests.
I'm using maven project where single test case is a java class, and consists of steps which every of them is also a java class. it looks like this:
public class FirstTestCase() {
new Step1(driver).run();
new Step2(driver).run();
new Step3(driver).run();
}
public class SecondTestCase() {
… second test case steps ...
}
In every step - „run” - contains selenium code.
Individual test cases are grouped into larger sets. For example:
#RunWith(Suite.class)
#Suite.SuiteClasses({
FirstTestCase.class,
SecondTestCase.class
})
public class SetOfTests {}
Finally, I have one major class (which is called “TestRunner”) in which the respective sets are activated. It looks like this:
public class TestRunner {
#Test
public void main() {
Result result = JUnitCore.runClasses(
SetOfTests.class
);
So, class hierarchy in my project looks like:
TestRunner |-
SetOfTests |-
FirstTestCase |-
|- Step 1
|- Step 2
|- ....
SecondTestCase |-
|- Step 1
|-Step 2
|- ....
Now, my question is:
How will be best approach to add Allure annotations (like a #Stories,#Features, #Steps... ) that
after the Jenkins “build”, when report will be generated, result will be presented in form as a hierarchy above
Note that, jenkins is configured, works and also generates a report but not in such a form as I want.
Just I need to know in simple way in which test and which step the error occurred
If you look at Allure XSD file you will see that currently (1.4.x series) we support the following structure:
TestSuite|
TestCase|
Step|
Substep|
...
So test suite can contain a set of test cases (nested test cases are not supported) and each test case can contain a hierarchy of steps with unlimited depth. I would implement a custom JUnit adapter for your case - this is as simple as creating one Java class like the following which implements JUnit RunListener interface.
I've got a utility class that I've created:
package com.g2.quizification.utils;
import com.g2.quizification.domain.Question;
public class ParsingUtils {
public static Question parse(String raw) {
Question question = new Question();
//TODO: parse some stuff
return question;
}
}
...that lives here:
I've also followed the tutorials and created a testing app, that looks like this:
And here's my test code, just waiting for some good 'ole TDD:
package com.g2.quizification.utils.test;
import com.g2.quizification.domain.Question;
import com.g2.quizification.utils.ParsingUtils;
public class ParsingUtilsTest {
public void testParse() {
String raw = "Q:Question? A:Answer.";
Question question = ParsingUtils.parse(raw);
//assertEquals("Question?", question.getQuestion());
//assertEquals("Answer.", question.getAnswer());
}
}
The test class is obviously missing the extension, but all the examples seem to only show extending something like ActivityUnitTestCase. I'm not testing an activity; I just want to test a static method in a utility class. Is that possible?
It seems like creating a utility test class should be simple, but I'm not sure what the next step is and/or what I'm missing.
The best approach for test project is to add the test project so that its root directory tests/ is at the same level as the src/ directory of the main application's project. If you are using junit4 and eclipse, you can just right-click on the util class you want to test and choose New -> JUnit Test Case.
Basically I would expect a new test class named ParsingUtilTest under the source folder tests/ and within the package com.g2.quizification.utils.test. The test class should extend TestCase and each method you want to test in that util class should have a new method in the test class with the name preceded with "test". I mean to say, suppose you have a method name in ParsingUtils called parseXml. The test method name in ParsingUtilsTest (which Extend 'TestCase') should be named testParseXml
The test class is obviously missing the extension, but all the examples seem to only show extending something like ActivityUnitTestCase. I'm not testing an activity; I just want to test a static method in a utility class. Is that possible?
Yes, as long as the class your are testing has nothing to do with android apis. And if you do need to test code with android api dependencies, for example, testing a view or an activity, you might want to have a try with robolectric. It's faster than the ones that extend ActivityUnitTestCase.
I have been playing with robolectric a lot (to do TDD on android), and so far, I prefer version 1.1 or 1.2 to 2.x, more stable and run fast.
Besides the tools mentioned above, there are many practices for writing good test cases, naming conventions, code refactoring and such.
It seems like creating a utility test class should be simple, but I'm not sure what the next step is and/or what I'm missing.
Its good to begin with small steps, xUnit Test Patterns: Refactoring Test Code and Extreme Programming Explained are some good books for your reference.