简体   繁体   中英

How to capture SQL statements of NHibernate with resharper and xunit 2

I´m trying to display the sql statements of nhibernate from my XUnit 2.x unittests in the Resharper Output of the Unit Test Sessions, but it does log the sql statements.

With MSpec Tests there is no issue and the sql statements are shown. With XUnit 1.x I think the sql statements were also logged.

I have configured NHibernate via property show_sql

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

There was a change in XUnit 2 how output is captured, but I not sure how to combine this with NHibernate to log the sql statements.

So has anyone a solution for this? I´m trying to avoid log4net integration in my unittests only to log these statements.

XUnit 2.1, NHibernate 4.0, ReSharper 2016.3.1, Visual Studio 2013

Thanks to @David Osborne´s comment I use an EmptyInterceptor to capture the SQL statements of NHibernate and write them to the ITestOutputHelper provided by the XUnit framework.

public class XUnitSqlCaptureInterceptor : EmptyInterceptor
{
    public XUnitSqlCaptureInterceptor(ITestOutputHelper output)
    {
        this.Output = output;
    }

    public ITestOutputHelper Output { get; set; }

    public override SqlString OnPrepareStatement(SqlString sql)
    {
        this.Output.WriteLine(sql.ToString());

        return sql;
    }
}

The usage is as follows:

[Fact]
public void MyUnitTestWithSQL()
{
    using (var session = factory.OpenSession(new XUnitSqlCaptureInterceptor(this.output)))
    {
        using (var transcation = session.BeginTransaction())
        {

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM