How do I put new List<int> {1} in an NUNIT TestCase?

add to list python
list add java
list.add c#
python list
python list insert
python list of lists
list.append python 3
python list extend

I have the method:

public static int Add(List<int> numbers)
    {
        if (numbers == null || numbers.Count == 0)
            return 0;

        if (numbers.Count == 1)
            return numbers[0];


        throw new NotImplementedException();
    }

Here is my test against it, but it does not like new List<int> {1} in the TestCase:

    [TestCase(new List<int>{1}, 1)]
    public void Add_WithOneNumber_ReturnsNumber(List<int> numbers)
    {

        var result = CalculatorLibrary.CalculatorFunctions.Add(numbers);

        Assert.AreEqual(1, result);
    }

It gives me the error:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

Do I have to do it like this:

    [Test]
    public void Add_WithOneNumber_ReturnsNumber()
    {

        var result = CalculatorLibrary.CalculatorFunctions.Add(new List<int>{7});


        Assert.AreEqual(7, result);

        var result2 = CalculatorLibrary.CalculatorFunctions.Add(new List<int> {3});

        Assert.AreEqual(4,result2);
    }

There is one option to use TestCaseSource attribute. Here I provide a non-assert test with two cases just to see how it works:

[TestFixture]
public class TestClass
{
    private static readonly object[] _sourceLists = 
    {
        new object[] {new List<int> {1}},   //case 1
        new object[] {new List<int> {1, 2}} //case 2
    };

    [TestCaseSource("_sourceLists")]
    public void Test(List<int> list)
    {
        foreach (var item in list)
            Console.WriteLine(item);
    }
}

Anyhow I have to mention it is not the most evident solution and I would prefer neatly organized fixtures ignoring the fact they are more verbose

More information: https://github.com/nunit/docs/wiki/TestCaseSource-Attribute

We have three methods to add new elements to a list: append() , insert() , and extend() . langs = []. An empty list is created. langs.append("Python") langs.​append("  We add 4 prime numbers to each List. The values are stored in the order added—2, 3, 5 and then 7. Version 1: We call Add () 4 times with the number as the argument. The end count of the list is 4. Add. Version 2: This code adds all 4 numbers in a single expression—it is easier to read, and the code generated is the same.

My solution is simpler, I just use params. I hope this works for you!

[TestCase(1, 1)]
[TestCase(10, 5, 1, 4)]
[TestCase(25, 3, 5, 5, 12)]
public void Linq_Add_ShouldSumAllTheNumbers(int expected, params int[] numbers)
{
    var result = CalculatorLibrary.CalculatorFunctions.Add(numbers);
    Assert.AreEqual(expected, result);
}

Here is an example of adding elements to a Java List using the add() method: List<String> listA = new ArrayList<>(); listA.add("element 1");  Active Oldest Votes. 275. Use List.AddRange (collection As IEnumerable (Of T)) method. It allows you to append at the end of your list another collection/list. Example: List<string> initialList = new List<string>(); // Put whatever you want in the initial list List<string> listToAdd = new List<string>(); // Put whatever you want in the second list initialList.AddRange(listToAdd);

I often use strings and parsing as it renders nicely in the testrunner. Sample:

[TestCase("1, 2")]
[TestCase("1, 2, 3")]
public void WithStrings(string listString)
{
    var list = listString.Split(',')
                         .Select(int.Parse)
                         .ToList();
    ...
}

Looks like this in Resharper's runner:

In this tutorial we will see how to copy and add all the elements of a list to al = new ArrayList<String>(); //Adding elements to the ArrayList al.add("Text 1");  To add a title to your My List, follow the steps below. On the website: Hover over a TV show or movie and select the + option, or select a TV show or movie and select + My List. On your Netflix ready device: Select a TV show or movie, then select Add to My List or + My List.

Improve code for @Yurii Hohan answer:

private  static readonly object[] _Data =
        {
            new object[] {new List<int> {0}, "test"},
            new object[] {new List<int> {0, 5}, "test this"},
        };

[Test, TestCaseSource(nameof(_Data))]

Hope this help.

C# List class represents a collection of a type in C#. Create a list; List<string> AuthorList = new List<string>();; // Add items using Add method  To determine whether your site is currently included in Google's index, do a site: search for your site's URL. For example, a search for "site:wikipedia.org" returns these results. My site isn't on Google! Although Google crawls billions of pages, it's inevitable that some sites will be missed. When our spiders miss a site, it's frequently for

You can use this :

[TestCase(new []{1,2,3})]

public void Add_WithOneNumber_ReturnsNumber(int[] numbers)

Map<String, List> mainMap = new HashMap<String, List>(); for(int i=0; i<​something.size(); i++){ Set set = getSet(); mainMap.put(differentKeyName, Arrays. To create a new list: From your News Feed, go to the Explore section on the left and click Friend Lists. If you don't see it, click See More then scroll to find it. Click + Create List.

In this short tutorial, you will learn how to add an item to a list in Python using insert and append Duration: 1:43 Posted: May 18, 2018 To create a new list: From your News Feed, go to the Explore section on the left and click Friend Lists. If you don't see it, click See More then scroll to find it. Click + Create List.

in R programming. You will learn to create, access, modify and delete list components. How to add components to a list? Adding new components is easy. We simply assign values using new tags and it will pop into action. > x[["​married"]]  To create a new contact list, follow these steps: Select the Lists option from the Dashboard toolbar. In Lists, click the Create list button. Enter the list name in the field provided. Please remember that the name must be unique – it can’t exist in any GetResponse account. The name must be between 3-64 characters (only lowercase a-z, numbers, minus (-), and underscore (_).

The expressions can be anything; you can put all kinds of objects in lists, including other And Python never creates a new list if you assign a list to a variable. Click the link below, input your property address, and select “Continue” to get started on creating your For Sale by Owner ad: This will walk you through entering your home and listing details. Then, your listing will be immediately sent to our verification team for approval.

Comments
  • you will not be able to use non-constant expression in TestCase
  • @YuriiHohan - Is my only option the new Test I posted?
  • You may want to use TestCaseSource here instead of TestCase nunit.org/index.php?p=testCaseSource&r=2.5
  • Can you clarify your last comment with an example? And is it possible to actually call another test from another test or is this bad practice?
  • By the way, this worked, but I do agree it is a little strange. I probably would rather have more concise and neat tests.
  • I don't understand the value of creating a private array of lists in the class, only to reference them via an attribute. Not trying to be rude, just not understanding why this is any better than just having no parameters to the Test() method and generating the list in the test method itself. Does this list get re-used in other test methods? If so, then wouldn't a Setup() type method be more useful or no?
  • It is not better which I state in the answer, it is just the way of doing it that the library allows
  • Update for your comment, use: private static readonly object[] _Data = { new object[] {new List<int> {0}, "test"}, new object[] {new List<int> {0, 5}, "test this"}, }; [Test, TestCaseSource(nameof(_Data))]
  • That doesn't answer the question. You are using primitives which are constant. Try it with anything but a compile time constant.
  • That's not a list, it's an array. OP would have to convert this to a list.
  • Does this mean that I have to have to have a separate test for 3 and 7 and can't use TestCase?
  • If 3 and 7 test different things, then yes you will need to have different tests cases for each. If they are just two values that both exercise the same test, then no.
  • How can I include 3 and 7 in my Test? The only way I can think is to instantiate a List for each number and call the Add Method, so basically, my Test is running like a TestCase. Do I have that correct?
  • Is my only option the new Test I posted?