unit testing a bison generated parser - c++

I have eclipse with cdt and a project which contains files generated by flex and bison, the project runs perfectly but I couldn't run it as a unit test, here the things I tried:
CUTE
As the answer on this question says, I couldn't find a way to test a project that already exists, so this didn't work.
CppUnit is old as the answer said(didn't try it though but I don't want an old thing).
CDT unit testing
As this question states, unit testing is now a part of cdt, I installed it and installed googletest and followed the instructions but when I run it, it says : Unknown error during parsing Google Test module output : Unexpected test module output.
This is another question getting the same message but it doesn't have an answer(except in comments which is the same answer I followed when setting up cdt unit testing).
and after I ran this test, I got compiler errors in yacc.cpp and lex.yy.cc which I didn't get when running the project in normal.
Goal
I'm a university student doing a project with four collegues, we're building a compiler to a language specified by the professors.
I have seen a question on "bison flex unit testing", this is it : Unit test of flex bison scanner parse, how to drive the test case
but the poster of the question is using C, we're restricted to use C++ in both scanner and parser, so the question can't really help me.
PS
because running the project without unit testing worked well, I thought the error isn't with my yacc.y or lex.l, that's why I didn't post them, if they are needed I'll edit my question.
One last thing, I'm running linux mint 17.1 64bit.

Related

Boost testrunner in Eclipse fails to generate valid XML

I am attempting to run unit tests from Eclipse on C++ code. My Eclipse is Mars.2 Release (4.5.2). I originally downloaded the Java Developer's version, but I've installed C/C++ plugins. I wrote a simple class and then a boost unit test for that class. I can compile the code both in Eclipse and on the command line to get an executable. If I run the executable on the command line, I get this output
Running 1 test case...
*** No errors detected
So far so good.
Now if I try to run inside Eclipse, I get an error. At first it appeared that my test was not running at all. In the "C/C++ Unit" tab, I got a red bar on the right side of the screen, no information where I should be seeing individual test results, and a message "XML parse error: Premature end of file" in small text right below the row of tabs. By stopping in the debugger, I was able to see that I'm victim of a known bug. Basically there's some incompatibility between Eclipse and boost testrunner, where one is specifying XML and the other xml (different case) for the format. I tried this work-around mentioned in the discussion about the bug at that link:
As a workaround you can add following arguments in the unittests' run configuration:
--output_format=XML --log_level=all --report_level=no --
Eclipse's test runner appends it's arguments to these. Arguments after -- are passed on to your test script and ignored by Boost.Test.
With that in place, I still have problems, but the problem is different depending on whether I run with debugging or not. Without debugging, the result is the same as noted above. With debugging, I get information about my test - noting that it ran and passed - on the left side of the "C/C++ Unit" tab but I still get the red bar indicating a failure. The small text in this case says: "XML parse error: Content is not allowed in tailing section."
The bug noted above appears to be about 2 yrs old, although the discussion has activity as recently as last month. I guess there's some other work around? It seems likely that the community has been running tests in the intervening two years!
I tried updating Eclipse via Help -> Check for Updates. That did not improve my situation. I suspect that other information may be valuable to help, but I'm not sure what to provide. I'll watch comments and update if / where I can.
Reluctantly, I upgraded to the Neon.2 release of Eclipse. The underlying bug is still present, as described in my question, but the work-around noted works in that release. Copying that down from the question for completeness:
As a workaround you can add following arguments in the unittests' run configuration:
--output_format=XML --log_level=all --report_level=no --
Eclipse's test runner appends it's arguments to these. Arguments after -- are passed on to your test script and ignored by Boost.Test.
Would have been nice if this worked in Mars as well since upgrading is a bit of a hassle - especially with support for multiple development languages - but at least I can move on. Probably would have had to upgrade at some point in any case.

Unable to reliably test a basic class c++

I have recently started to try to set up unit testing for my project. However, it is not working properly for classes.
I set it up using the directions from the question Unresolved externals when compiling unit tests for Visual C++ 2012.
What will happen is that the tests will start fine, but then there will be a memory access error (C0000005). This does not happen all of the time though. It will generally fail every two or three attempts, but can fail as often as one in every 20. It does not seem to matter what other functions are in the code.
When I try to debug the test, the first time a variable is accessed or used, the test will fail.
I am trying out two different testing frameworks, Boost.test (using Boost Unit Test Adapter) and Native Unit Tests. Both of them are having the same problems.
I know that the dll and the test application both use the same calling convention (__cdecl).
What is bizarre is that the test will work for a while, then suddenly begin to fail.
The project is available here.
Reinstalling Visual Studios did not help.
Whatever is going on, it's almost certainly in your code as many, many people use Boost.Test successfully with Visual Studio. Instead of a 100+ MB zip file on dropbox that noone is going to download, try to simplify your problem to the smallest possible bit of code that reproduces the problem and post it in your question.
Here's a more constructive answer. Try working through the 5-part tutorial I wrote on test-driven development with Boost.Test and Visual Studio:
C++ Unit Tests With Boost.Test, Part 1
C++ Unit Tests With Boost.Test, Part 2
C++ Unit Tests With Boost.Test, Part 3
C++ Unit Tests With Boost.Test, Part 4
C++ Unit Tests With Boost.Test, Part 5
You can also look at a workshop that I presented at C++ Now! 2014, which covers the use of the Turtle Mock library for mocking collaborators. I used CMake for the build system in the workshop as it was unreasonable to assume Visual Studio for that audience.
Working through either (or both) of those examples should give you enough step-by-step instructions that you should be able to identify what you are doing wrong in your project.

Eclipse (CDT) plugin for running tests and browsing report

Google's answers (hear! hear!) and Eclipse Market Place search results on this topic simply drive me crazy! And apparently the proposed SO answers aren't really helpful either.
I'm looking for an Eclipse plugin, that allows me to browse a JUnit report XML compliant unit test report produced from a google test runner.
Nice to have features:
jump to the source from failure reports
run the tests automatically after building.
I'm pretty sure a (free) plugin suitable for Eclipse CDT exists, that realizes these requirements, I'm just too stupid to find it :-). May be any smarter guy/gal here?
Since JUNO there is a C++ Unit testing support package. It is package of the CDT. I only used it with boost test, but AFAIK it also supports googletest.
#mkaes comment pushed me to it!
Install the CDT (Help->Install Software->CDT) optional C/C++ Unit Test support
Create a new Runner using 'Run As -> Run Configurations -> C/C++ Unit Test
Choose 'Google Tests Runner' for 'Tests Runner' in the 'C/C++ Testing' tab
Choose your testing project executable in the 'Main' tab
Run it, and the 'C/C++ Unit' view shows up after (rebuild and) execution
Look here for more illustrated and detailed instructions: Eclipse Juno has landed with unit testing support for C++

Running BOOST unit tests from Visual Studio 2010

I am just starting to work with QuantLib project. There are quite a few BOOST unit tests defined, hence the question: is there an easy way to run these tests selectively from Visual Studio (and see results)?
I've only used unit testing in .Net languages so far, and there you can either use the built-in test runner if you use MsTest, or some add-on (I used Resharper and TestDriven.NET) for other frameworks. There you can choose which tests you run, and you can run them with the debugger attached (which is unfortunately useful sometimes). Are there similar add-ons for BOOST tests in unmanaged C++?
Yes you can!
If your boost unit test project is defined correctly, then you can run the entire test project from VS10 debugger by clicking
right click on project --> Debug --> Start new instance
This will run the tests inside VS framework which will give you all the debugger goodies.
For running specific test suites/cases, just edit the project command line arguments under
right click on project --> Properties --> General --> Debugging
and add the parameters according to the links in the other answers
you can run any test case or test suite via command line. running tests by name
it even allows wildcards.
ReSharper C++ supports discovery and selective running/debugging of Boost tests (with Boost 1.59 and later).
I'm using the Boost Unit Test Adapter. It's free and has a very nice output. It can also list the time the single tests consumed and you can start single tests and group of tests.
You can download it here:
https://visualstudiogallery.msdn.microsoft.com/5f4ae1bd-b769-410e-8238-fb30beda987f?SRC=VSIDE

Where to find the parsed Boost.Test output in Eclipse

There is already a thread here that partially answers my question .
On Eclipse 3.7.2 I followed the approach provided there and I could successfully accomplish the steps creating and setting up a new error parser and adding it to my current project. After executing my Boost.Test (boost rel. 1.48.0) Unit Test, on the Eclipse console I get the same output as the output I get when no parsing is done (e.g. when executing the Unit Test outside Eclipse (e.g. on a Linux terminal)). I searched for a new Eclipse console where the parsed Unit Test output could be displayed (similar to the consoles by e.g. gcov, gprof or cppcheck in Eclipse) but found nothing alike.
Where should the parsed unit test output be displayed? In case the parsed output shall be displayed in the Eclipse Console view, are there any suggestions what might have gone wrong with the parsing in my case?
Thanks in advance.
P.S.: Thanks to casperOne and kleopatra for teaching me manners.
If you're using Eclipse there is also a really nice plugin called TestRunner for running CDT unit tests much like Java or Python unit tests. It handles the unit-testing parsing for you. You'll get a separate console window for your unit tests and it arranges them with level filters. Clicking on an error/warning takes you to the unit-test line number in your editor. You also can set the verbosity level as well as a few other settings from inside Eclipse.
You can directly install the plugin by using the following link in Eclipse updates https://raw.github.com/xgsa/cdt-tests-runner/tests_runner_demo/testsrunner/org.eclipse.cdt.testsrunner-updatesite/site.xml
In order to eclipse parse errors, the error must be print in the console view at compile time. If you are emitting errors in runtime, you must add your program to be called by the Makefile.
That's how I do in embedded systems.