i have phpunit test that directly interect with database and when assert fail it stop test and don't pass to the nest assert - phpunit

I have PHPUnit test that directly interacts with database and when the assert fails, it throws an error and then stops the test and doesn't execute the next assert.
What I need is that when an assert fails, it should pass to the next assert and throw an error at the end of the test.
My biggest issue is because I am interacting with database, when I make Insert, Update, GetAll, GetbyID at the end of test I always make Delete.
I know there other ways of testing without interacting with the database but I was asked to make a test that interacts with the database, so what I need something like (SOFT ASSERT-TRY CATCH) in PHPUnit.
So please anyone that has an answer help me.

If you have a configuration file:
<phpunit
stopOnError="false"
stopOnFailure="false">
Set those two attributes to false, or if running off the command line use
--stop-on-error
--stop-on-failure

Related

How to find out which setup failed in Assert of `VerifyAll`?

I am writing the unit test for a method. I have properly configured all the setups. While debugging unit test it works and returns values as expected. But in assertion it still throws false.
Therefore, I am not able to understand why does VerifyAll throws false all the time? How to find out which setup failed the VerifyAll assert?
VerifyAll verifies that all your Setups were called. It sounds like you have a Setup that is not relevant to your test, hence your are getting the expected output, yet the VerifyAll fails because that Setup was never called.
When VerifyAll fails you will get an exception, and the reason is given there. For example, if I create an unused Setup in a test I get:
Moq.MockVerificationException : The following setups were not matched:
IMyClass m => m.MyMethod()

SpecFlow - The given key was not present in the dictionary

I am facing a strange issue with my Specflow test scenario. In 'When' step, I am setting ScenarioContext as below to pass data:
ScenarioContext.Current.Add("mykey",ListOfStrings);
In 'Then' step,I am retrieving my collection from the scenarioContext as below
ScenarioContext.Current.Get<IList<string>>("mykey");
Its working fine on my local machine but when I trigger VSTS build it throw exception
"System.Collections.Generic.KeyNotFoundException: The Given key was not presented in the dictionary"
The reason I found for this exception is ScenarioContext dictionary becomes null in 'Then' step.
Though, in same project, other similar test case scenarios working fine where I am using ScenarioContext in same way. But in this specific test scenario I am facing this issue where SenarioContext get reset to null in 'Then' step and throw exception during VSTS Build. Any suggestion would be appreciated.
The ScenarioContext inherits from Dictionary. When you debug, is this dictionary really empty? Do you have perhaps somewhere a ScenarioContext.Current.Clear() in your code?
My suggestion is to do not use the ScenarioContext for saving state, but to use Context Injection (http://specflow.org/documentation/Context-Injection/) for it.

RocksDB cryptic error message

Does anyone understand what this RocksDB error refers to ?
/column_family.cc:275: rocksdb::ColumnFamilyData::~ColumnFamilyData():
Assertion `refs_ == 0' failed. Aborted (core dumped)
This is an assertion failure raised by RocksDB, and it intentionally terminates the execution of the program.
In general, assertions are used by programmers to ensure certain invariants in the program. Assertions have some runtime overhead, and therefore can be completely disabled. Often they are compiled into development or debug builds, but are omitted for production builds.
When an assertion fails, the program execution is intentionally aborted immediately by calling std::abort. This may lead to your OS writing a core dump (as it obviously did as the above message reveals), but if and where core dumps are written depends on the OS configuration.
In case of this specific assertion, the destructor of rocksdb::ColumnFamilyData raised the assertion because it requires its refs_ member to have a value of 0. refs_ is a reference counter and it makes sense to assert that no references are actually held when the object's destructor is called.
From just looking at the destructor code, it is unclear whether this is a bug in the RocksDB library itself, or an error caused by using it the wrong way, e.g. destroying column family objects when they are still in use by other objects.
For reference, here's the code part that raised the assertion (currently on line 365 in file rocksdb/db/column_family.cc):
ColumnFamilyData::~ColumnFamilyData() {
assert(refs_.load(std::memory_order_relaxed) == 0);
If the error persists, it may be useful if you provide the code that uses RocksDB here. Otherwise it may be impossible to find the error source.
The core dump may also provide useful information, because it contains the stack trace of the code that actually invoked the object's destructor.
I noticed that all column_family.cc errors (core_dumped, memory_order_relaxed and etc) occur after incorrect rocksdb installation. In my vagrant script i found true way.
instead of use
https://github.com/facebook/rocksdb/blob/master/INSTALL.md
i create script
cd /opt
git clone https://github.com/facebook/rocksdb.git
cd rocksdb
git checkout tags/v4.1
PORTABLE=1 make shared_lib
export LD_LIBRARY_PATH=/opt/rocksdb
LD_LIBRARY_PATH add better to your environment path(.bash_rc or /etc/environment)
Assertion refs_ == 0 fails on ~ColumnFamilyData() means the reference count of a column family is not zero when the column family is deleted. Most likely you have some un-deleted column family handles before closing the DB. Note that all column family handles must be deleted before closing the DB. Otherwise the assertion will fail.
// Before delete DB, you have to close All column families by calling
// DestroyColumnFamilyHandle() with all the handles.
static Status Open(const DBOptions& db_options, const std::string& name,
const std::vector<ColumnFamilyDescriptor>& column_families,
std::vector<ColumnFamilyHandle*>* handles, DB** dbptr);
To fix such assertion failure, making sure you delete all column family handles before closing the DB.

Taking screenshot in robot framework on step (keyword) failure

I am testing a GUI application with robot framework and AutoItLibrary. I would like the test to take screenshot after any failure in test, but not in the test teardown, but right after the error occurs, as the steps in between the failure and the teardown usually make the screen worth nothing. Is there a way to do that in robot? All I could get so far is a screenshot in the end of a test...
Thanks in advance!
AutoIt lib has CaptureScreenOnError (defaults to False) argument so when importing the lib, set it to True. See https://robotframework-autoitlibrary.googlecode.com/svn/tags/robotframework-AutoItLibrary-1.1/doc/AutoItLibrary.html
It will not take screenshot if the error happens in non-AutoIt lib keyword. AFAIK there isn't any nice and sure way to take screenshot after any failure.
Run Keyword If Test Failed Take Screenshot
in every test teardown will handle most situation, but if the error happens in a keyword that has a teardown, the keyword teardown will be done before the test teardown. And "Run Keyword If Test Failed" will only work in test teardown.

How to stop running tests if a specific test fails?

I know there is an option in PHPUnit to stop-on-failure but I don't want it to stop when any test fails, just when this specific test fails.
For example, in my setUp I connect to a DB, and in the first test I check if it connected to the correct DB. If that fails, I can't run the rest of the tests.
Use the #depends feature of PHP.
If a test depends on another, it will only be executed if that other test succeeded. Otherwise it will be skipped. This allows you to pinpoint problems better.
Usage: Add a PHPDOC block at the top of the test function that should only be executed when the other test is successful, and add a line #depends testConnectToDb.
See http://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.depends and http://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.test-dependencies for some more details.

Resources