Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initial tests for ExportFormat #1010

Merged
merged 4 commits into from
Mar 23, 2016
Merged

Conversation

oscargus
Copy link
Contributor

Really trivial and early test, but could maybe avoid bugs like #1001 to happen (and form a foundation to extend on).

@oscargus oscargus added testing status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers labels Mar 21, 2016
MetaData metaData = new MetaData();
exportFormat.performExport(db, metaData, filename, charset, entries);
} catch (IOException e) {
Assert.fail("Exception caught: " + e.toString() + e.getMessage());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably be more readable code if you just skip the try-catch here. The tests will fail anyway if the method is terminated by an exception and we will get all the exception info we need. There is no need to explicitly build an exception string as you do here.

Some code quality tools might unnecessarily complain about missing assertions, though...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lenhard That should be a general rule in my opinion, not to catch Exceptions ins Tests.
Edit// At the moment there are tests that fail but omit stacktrace. I'lll create a new issue for this

@tobiasdiez
Copy link
Member

Mhh....I am missing an assertion in these tests, i.e. reading the exported file and checking that it is empty.

@oscargus
Copy link
Contributor Author

I have changed the code based on all(?) comments. Major implications:

  • Now, all exporters returns an empty file for an empty set of entries and are tested for this. Earlier, some heading information was generated
  • All exporters are now tested that they throw an NPE with a null database or null entries list
  • The change that fixes error when exporting to endote and ris format #1001 is in practice reverted and solved such that the ExportFormat calls add all entries instead of null for all entries
  • The test disabled in Disabled NPE test #1005 is active again
  • I had to break the new guideline in Don't catch exceptions in Unit test #1012 to be able to test all exporters for NPEs in a simple way, if there's a better way please let me know

(This shows that adding a quite crappy initial test can lead to good things. :-))

@oscargus
Copy link
Contributor Author

As the export is now not performed for empty databases (entries lists), as a side effect, the coverage is more accurate now as well. Earlier it increased quite a bit, but no actual testing was done (well, apart from that the code executed without any errors).

@Test
public void testExportingEmptyDatabaseYieldsEmptyFile() throws Exception {
BibDatabase database = new BibDatabase();
for (Map.Entry<String, IExportFormat> exportFormats : ExportFormats.getExportFormats().entrySet()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use parametrized tests instead of the for loop. Then also the the string argument to assertEquals is no longer needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any good pointer/example? (It seemed like it should be possible somehow...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tobiasdiez
Copy link
Member

This looks way better then the initial PR. Good job!

@oscargus
Copy link
Contributor Author

Not obvious that Parameterized is executed before BeforeClass, but once I figured that out...

public void setUp() {
Globals.prefs = JabRefPreferences.getInstance();
ExportFormats.initAllExports();
@BeforeClass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think before is preferable to beforeclass since then every test gets a fresh database and the potentially modifications of the previous test do not influence the next test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK! I do not fully understand the difference and usually use Before. Now I just mimicked FormatterTest to see if it helped (but the problem was not related to that).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And regarding providing an actual database, the current method is not the best approach (with statics). However, it seems like the tests needs to be rather specific anyway for the different export formats so maybe they can't be parameterized in a good way anyhow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I second this. Every export format should have its own test class, which tests the actually export.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, is there a way to add a test which is not included in the coverage check? It would make sense to try to export a non-empty database for every export format just to make sure that no exceptions are thrown, but probably one wouldn't like to include these test in the coverage report...

@tobiasdiez
Copy link
Member

LGTM

@oscargus oscargus merged commit e477631 into JabRef:master Mar 23, 2016
@oscargus oscargus deleted the exportformattest branch March 23, 2016 17:41
@oscargus
Copy link
Contributor Author

I think this is probably as far as we get right now and that the code is quite OK since it is rather general and only tests what is actually tested (or how to formulate it...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants