Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Report diagnostic for use of (some) Assert methods with IQuerable<T>

Closed
#11,872 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp
Domain
devtools, testing

Research direction

The issue refers to MSTEST0037, an existing analyzer rule that already reports some Assert calls. Start by finding that rule and how it inspects the argument type, then check whether it can be extended to IQueryable<T> and the listed methods (IsEmpty, IsNotEmpty, HasCount, ContainsSingle). Done means the listed BAD cases report a diagnostic while the GOOD Count/Any cases stay silent, with tests for both.

Written by the indexing model from the issue text.

Description

area/analyzers needs/triage

Summary

MSTEST0037 is currently not reported when the argument is an IQueryable<T> or a result of a method invoked on an IQueryable<T> (eg. IQueryable<T>.Count(...)).

This is great of course!

That said, people till tend or risk to use Assert.IsNotEmpty(...), Assert.IsEmpty(...) , Assert.ContainsSingle(...) or Assert.HasCount(...) with an IQueryable<T>.

This will of course lead to a serious performance degradation as filtering will be done client-side and counting of the number of results will be done on the dataset returned by the database (instead of just having off a SELECT COUNT(1) FROM .... to the database).

I haven't had time to review all Assert method. I'm sure this applies to other methods as well.

Would it be possible to have an analyzer rule that reports a diagnostic for at least the following cases:

private IQueryable<PERSON> GetPersons()
{
    // DB stuff
}

[TestMethod]
public void SomeTest()
{
    var persons = GetPersons();
    
    Assert.AreEqual(1, persons.Count(p => p.NAME == "John")); // GOOD
    Assert.HasCount(1, persons.Where(p => p.NAME == "John")); // BAD
    Assert.ContainsSingle(persons.Where(p => p.NAME == "John")); // BAD

    Assert.AreEqual(0, persons.Count(p => p.NAME == "John")); // GOOD
    Assert.IsEmpty(persons.Where(p => p.NAME == "John")); // BAD

    Assert.AreNotEqual(0, persons.Count(p => p.NAME == "John")); // GOOD
    Assert.IsNotEmpty(persons.Where(p => p.NAME == "John")); // BAD

    Assert.IsTrue(persons.Any(p => p.NAME == "John")); // GOOD
    Assert.IsNotEmpty(persons.Where(p => p.NAME == "John")); // BAD

    Assert.IsFalse(persons.Any(p => p.NAME == "John")); // GOOD
    Assert.IsEmpty(persons.Where(p => p.NAME == "John")); // BAD
}

Any call to Assert.ContainsSingle(...) may be ok if caller actually uses the returned value.

Dominant language
C#
Stars
1k
Forks
314
Avg merge
8h 39m
Merged PRs (30d)
446

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/testfx

All issues in microsoft/testfx

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.