1

I have a test that creates new transaction and checks that it's returning 200:

  [Test]
        public async Task CreateNewTransactionReturns200()
        {
            var json = JsonReader.ReadJson("Transaction");
            var jObject = JObject.Parse(json);
            json = jObject.ToString();

            var content = new StringContent(json, Encoding.UTF8, "application/json");
            var response = await HttpClient.PostAsync(CreateNewTransactionURL, content);

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }

Transaction is like:

 "Transaction": {
    "type": "transaction",
    "amount": "100"
  },

Now I need to add new test that creates 100 transactions and I try to do it in next way:

  [Test]
            public async Task Create100TransactionsReturns200()
            {
                var json = JsonReader.ReadJson("Transaction");
                var jObject = JObject.Parse(json);
                json = jObject.ToString();
       
               List<string> list = new List<string>();

               for (int i = 0; i < 100; i++)
              {
               list.Add(json);
               }
                var content = new StringContent(list, Encoding.UTF8, "application/json");
                var response = await HttpClient.PostAsync(CreateNewTransactionURL, content);
    
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            }

or this way

   [Test]
                    public async Task Create100TransactionsReturns200()
                    {
                        var json = JsonReader.ReadJson("Transaction");
                        var jObject = JObject.Parse(json);
                        json = jObject.ToString();
                        string transactions = "";

                        for (int i = 0; i < 100; i++)
                        {
                        transactions += json;
                       }
                       var content = new StringContent(transactions, Encoding.UTF8, 
                       "application/json");
                    var response = await HttpClient.PostAsync(CreateNewTransactionURL, content);
        
                    Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
                }

But the first gives me underlined list in 'content' variable, and second creates 1 transaction. How to add 100 transactions in a row?

Natali
  • 45
  • 5
  • Maby take a look at: https://stackoverflow.com/questions/532892/can-i-multiply-a-string-in-c – Daniel Stackenland Oct 22 '21 at 06:31
  • Does the API supports creating one transaction in one request or multiple transactions in one request? If it supports only one transaction creation in one request then you need to call the API 100 times in for loop. – Chetan Oct 22 '21 at 06:35
  • as I know it supports 1 transaction per one request. How to call i 100 times in a loop? (I tried it too but somehow it didn't work for me) – Natali Oct 22 '21 at 06:37

1 Answers1

1

From your comments, you most probably need this:

  [Test]
  public async Task Create100TransactionsReturns200()
  {
      var json = JsonReader.ReadJson("Transaction");
      var jObject = JObject.Parse(json);
      json = jObject.ToString();


      for (int i = 0; i < 100; i++)
      {
         var content = new StringContent(list, Encoding.UTF8, "application/json");
         var response = await HttpClient.PostAsync(CreateNewTransactionURL, content);
    
          Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
       }

   }

But why would you do this? Is this some kind of performance test you are trying to achieve? If yes, then that's definitely not the way to go even though it might give you some metrics.

At the very least, I would expect some type of time-passed assertion.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61