1

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

Jehof
  • 34,674
  • 10
  • 123
  • 155
  • It might be possible with xUnit's `ITestOutputHelper` and an interceptor as suggested here: http://stackoverflow.com/a/2874025/1162077 – David Osborne Jan 06 '17 at 10:17

1 Answers1

1

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())
        {

        }
    }
}
Jehof
  • 34,674
  • 10
  • 123
  • 155