Recent Changes - Search:

Oktatás

* Programozás 2
  + feladatsor
  + C feladatsor
  + Python feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

* Adator. prog.
  + feladatsor
  + quick link

Teaching

* Prog. for Data Sci.
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/1
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Unit tests: testing for exceptions

(1) testing for exceptions
import std.stdio;
import std.exception; // assertThrown(), assertNotThrown()

class MyException : Exception
{
    this(string msg = "")
    {
        super(msg);
    }
}

int add(const int a, const int b)
{
    if (a < 0 || b < 0)
    {
        throw new MyException("cannot be called with negative values");
    }
    //
    return a + b;
}

int product(const int a, const int b)
{
    return a * b;
}

void main()
{
    writeln(add(2, 3));
}

// ---

unittest
{
    assert(add(2, 3) == 5);
    assertThrown(add(-2, 3) == 1); // must throw an exception (of any type)
    assertThrown!MyException(add(-2, 3) == 1); // must throw this type of exception

    assert(product(2, 5) == 10);
    assert(product(2, 3) == 6);
    assertNotThrown(product(2, 3) == 19); // it only checks that product(2, 3) doesn't throw an exception
}

Running the unittest(s):

$ rdmd -unittest main.d
1 modules passed unittests

Simplification:

I have a fish function (ut.fish) that calls rdmd for unit testing. Usage example:

$ ut main.d
# rdmd -unittest main.d
1 modules passed unittests
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 September 06, 17:58